定義數組池及使用標誌位,以下以雙色球爲例:
1.定義數組池
定義紅球池
String[] m_RedPool = { "01", "02", "03", "04", "05", "06", "07", "08",
"09", "10", "11", "12", "13", "14", "15", "16", "17", "18",
"19", "20", "21", "22", "23", "24", "25", "26", "27", "28",
"29", "30", "31", "32", "33" };
定義紅球使用標誌位
boolean[] m_RedUsedFlag=new boolean[m_RedPool.length];
2.代碼
package DoubleBall;
import java.util.Arrays;
import java.util.Random;
public class DoubleBall {
/**
* @param args
*/
static String[] m_RedPool= { "01", "02", "03", "04", "05", "06", "07", "08",
"09", "10", "11", "12", "13", "14", "15", "16", "17", "18",
"19", "20", "21", "22", "23", "24", "25", "26", "27", "28",
"29", "30", "31", "32", "33" };
static String[] m_BluePool = new String[16];
static boolean[] m_RedUsedFlag=new boolean[m_RedPool.length];
static boolean[] m_BlueUsedFlag= new boolean[m_BluePool.length];
static String[] m_Ball = new String[7];
public static void main(String[] args) {
// TODO Auto-generated method stub
BluePool();
Ball();
}
private static void BluePool() {
for(int i=0;i<m_BluePool.length;i++){
char[] tmpBase={'0','0'};
String tmpBallIn=Integer.toString(i+1);
char[]tmpBall=tmpBallIn.toCharArray();
// System.out.println(Arrays.toString(tmpBall));
System.arraycopy(tmpBall, 0, tmpBase, tmpBase.length-tmpBall.length, tmpBall.length);
// System.out.println(Arrays.toString(tmpBase));
m_BluePool[i]=new String(tmpBase);
}
System.out.println(Arrays.toString(m_BluePool));
}
private static void Ball() {
Random tmpRandonBall=new Random();
int tmpBallNum;
for(int i=0;i<6;i++){
do {
tmpBallNum=tmpRandonBall.nextInt(33);
} while (m_RedUsedFlag[tmpBallNum]);
m_RedUsedFlag[tmpBallNum]=true;
m_Ball[i]=m_RedPool[tmpBallNum];
}
m_Ball[m_Ball.length-1]="99";
Arrays.sort(m_Ball);
// System.out.println(Arrays.toString(m_Ball));
m_Ball[m_Ball.length-1]=m_BluePool[tmpRandonBall.nextInt(16)];
System.out.println(Arrays.toString(m_Ball));
}
}