還是我來繼續jythoner的JAVA每日一題吧!最近比較閒哦!希望jythone不要見怪哦!
真是不好意思啊!早上有點忙!呵呵!
題目:一項抽獎程序要求讀者從整數1-49之間選擇6個不同的數字。編寫一個程序來完成這項工作,並生成5組結果。
package com.tengfei.lesson01;
public class Lottery {
public static void main(String[]args) {
int setCount = 5; // Number of sets of lucky numbers.
int setSize = 6; // Number of lucky numbers in the set.
int range = 49; // Assume selecting integers between 1 and range.
int lucky; // Holds a lucky number candidate.
int luckyCount; // Holds count of lucky numbers in a set.
for(int i = 0; i < setCount; i++) {
int lucky1 = 0; // Lucky numbers for the set of 6.
int lucky2 = 0;
int lucky3 = 0;
int lucky4 = 0;
int lucky5 = 0;
int lucky6 = 0;
luckyCount = 0; // Count of numbers found in the current set
while(luckyCount < setSize) {
// Generate a lucky number between 0 and 48 and add 1:
lucky = (int)(range*Math.random()) + 1;
switch(luckyCount) {
case 0: // It is the first one
lucky1 = lucky; // so just store it
luckyCount++; // and increment the count
break;
case 1: // For the second we must
if(lucky != lucky1) { // check that it is different from the first
lucky2 = lucky; // It is, so store it
luckyCount++; // and increment the count
}
break;
case 2: // For the third we check aginst the previous two
if(lucky != lucky1 && lucky != lucky2) {
lucky3 = lucky;
luckyCount++;
}
break;
case 3: // Check against the previous three...
if(lucky != lucky1 && lucky != lucky2 && lucky != lucky3) {
lucky4 = lucky;
luckyCount++;
}
break;
case 4: // Check against the previous four...
if(lucky != lucky1 && lucky != lucky2 && lucky != lucky3 && lucky != lucky4) {
lucky5 = lucky;
luckyCount++;
}
break;
case 5: // Check against the previous five...
if(lucky != lucky1 && lucky != lucky2 && lucky != lucky3 && lucky != lucky4 && lucky != lucky5) {
lucky6 = lucky;
luckyCount++;
}
break;
}
}
System.out.print("\nSet " + (i + 1) + ":"); // Identify the set
System.out.print(" " + lucky1 + " " + lucky2 + " " + lucky3 + // and output the numbers
" " + lucky4 + " " + lucky5 + " " + lucky6);
// If you want to be sure the numbers line up in columns you could use a
// rather more complicated statement here instead of the above:
/*
System.out.print((lucky1>9 ? " " :" ") + lucky1 +
(lucky2>9 ? " " :" ") + lucky2 +
(lucky3>9 ? " " :" ") + lucky3 +
(lucky4>9 ? " " :" ") + lucky4 +
(lucky5>9 ? " " :" ") + lucky5 +
(lucky6>9 ? " " :" ") + lucky6);
*/
// This makes use of the conditional operator to output an extra space
// when a lucky number is a single digit.
}
}
}