2011年11月7日 星期一

JAVA 大樂透 照大小排 以陣列達成

 

 

 

 

public class 魏功豪_作業_大樂透照大小排列 {
	public static void main(String[] args) {
		int betTimes = 6;
		int allBalls = 49;
		int [] arrSixBall = new int [6];
		//=============================
		for (int bt = 1; bt <= betTimes; bt++) {
			int [] arrOriginal = new int [allBalls];
			for (int i = 0; i <= arrOriginal.length - 1; i++) {
				arrOriginal[i] = i + 1;
			}
			System.out.print("第" + bt + "注\t");
			for (int times = 1; times <= 6; times++) {
				int randomTakeOneBall = (int)(Math.random()*arrOriginal.length);
				arrSixBall[times-1] = arrOriginal[randomTakeOneBall];
				int [] arrNew = new int [arrOriginal.length - 1];
				for (int i = 0; i <= (arrOriginal.length - 1) - 1; i++) {
					arrNew[i] = (i < randomTakeOneBall) ? arrOriginal[i] : arrOriginal[i + 1];
				}
				arrOriginal = arrNew;
			}
			for (int x = 1; x <= 6; x++) {
				int temp = arrSixBall[0];
				int arrNo = 0;
				for (int i = 1; i <=5; i++) {
					if (temp < arrSixBall[i]) {
						temp = arrSixBall[i];
						arrNo = i;
					}
				}
				System.out.print((temp < 10) ? " " + temp + ",\t" :temp + ",\t");
				arrSixBall[arrNo] = 0;    
			}
			System.out.print("\n");
		}
		//=============================
	}
}

沒有留言:

張貼留言