2011年11月7日 星期一

JAVA 大樂透 隨機取球 不重複 以陣列達成

 

 

 

 

public class 魏功豪_大樂透 {
	public static void main(String[] args) {
		int betTimes = 10;
		int balls = 49;
//================================================================================
		for (int bt = 1; bt <= betTimes; bt++) {
			int [] arrOrg = new int [balls];
			for (int i = 0; i <= balls-1; i++) {
				arrOrg[i] = i + 1;
			}
			for (int sixTime = 0; sixTime < 6; sixTime++) {
				int randomArrNo = (int)(Math.random()*(balls - sixTime));
				if (arrOrg[randomArrNo] < 10) {
					System.out.print(" ");
				}
				System.out.print(arrOrg[randomArrNo] + ",\t");
				int [] arrNew = new int [arrOrg.length -1];

				for (int i = 0; i <= arrNew.length -1; i++) {
					//arrNew[i] = (i < randomArrNo) ? arrOrg[i] : arrOrg[i + 1];//三源運算子寫法
					if (i < randomArrNo) {
						arrNew[i] = arrOrg[i];
					}
					else {
						arrNew[i] = arrOrg[i + 1];
					}
				}
				arrOrg = arrNew;
			}
			System.out.print("\n");
		}
//================================================================================
	}
}

沒有留言:

張貼留言