java中使用Math.random()的彩票游戏
我们被指派开发一个玩彩票的程序。该程序将随机生成两位数的彩票,并提示用户输入两位数。如果数字仅为一位数字(0-9),则必须将数字视为两位数字(00-09)。以下是条件:java中使用Math.random()的彩票游戏,java,if-statement,java.util.scanner,Java,If Statement,Java.util.scanner,我们被指派开发一个玩彩票的程序。该程序将随机生成两位数的彩票,并提示用户输入两位数。如果数字仅为一位数字(0-9),则必须将数字视为两位数字(00-09)。以下是条件: 如果用户输入与彩票号码的精确顺序匹配,则奖励为10万菲律宾比索 如果用户输入的所有数字与所有彩票号码匹配,则奖励为30000菲律宾比索 如果用户输入中只有一个数字与彩票号码匹配,则奖励为10000菲律宾比索 这是我到目前为止的代码…我实际上在第二个条件(奖励是PHP30000)上有一个问题,每当我只匹配一个号码时,它会说“你匹配
}这是一组毫无意义的测试。如果a不等于b,则不需要测试b是否等于a(不是)。您至少错过了一个条件(三个数字中有两个匹配);你可以用计数器来处理第一个之外的情况burrito77我确实做了那个代码我们的老师刚刚告诉我们条件是什么这个人猜对了所有三个数字,但没有按顺序猜对它们我对正确的代码没问题,但如果代码很难理解,然后我需要解释:)@AngelLyn注意,在这里和答案处进行如此长的对话表明您的问题信息太少,无法形成满足您所有要求的答案。因此,下次你应该花更多的时间考虑你的需求,然后把它们包括在问题中。你能解释一下你的变化吗?另外,突出显示它们。@Zabuza请看我们在上述问题下面的对话。@AngelLyn回答得好。我这样做是为了测试它们,而不是靠运气。@AngelLyn你知道代码是如何工作的,对吧?我知道我的。碰巧我的代码中有一些错误
public static void main(String[] args) {
Scanner lottery = new Scanner(System.in);
int lottery1, lottery2, lottery3;
System.out.println("\t\t\t\t\t\t~Welcome to THE LOTTERY~");
System.out.println("Enter your first two-digit lucky number!");
int guess1 = lottery.nextInt();
System.out.println("Enter your second two-digit lucky number!");
int guess2 = lottery.nextInt();
System.out.println("Enter your last two-digit lucky number!");
int guess3 = lottery.nextInt();
System.out.println("\nThe winning numbers are: ");
System.out.printf("%02d", lottery1 = (int)(Math.random() * 10));
System.out.printf("\n" + "%02d", lottery2 = (int)(Math.random() * 10));
System.out.printf("\n" + "%02d", lottery3 = (int)(Math.random() * 10));
if(guess1 == lottery1 && guess2 == lottery2 && guess3 == lottery3 ){
System.out.println("\n\nCongratulations! You matched all the lottery numbers in order.");
System.out.println("You won Php100,000!");
}else if(guess1 == lottery1 || lottery1 == guess1 && guess1 == lottery2 || lottery2 == guess1 && guess1 == lottery3 || lottery3 == guess1
&& guess2 == lottery1 || lottery1 == guess2 && guess2 == lottery2 || lottery2 == guess2 && guess2 == lottery3 || lottery3 == guess1
&& guess3 == lottery1 || lottery1 == guess3 && guess3 == lottery2 || lottery2 == guess3 && guess3 == lottery3 || lottery3 == guess3){
System.out.println("\n\nCongratulations! You matched all the lottery numbers.");
System.out.println("You won Php30,000!");
}else if(guess1 == lottery1 || guess1 == lottery2 || guess1 == lottery3
|| guess2 == lottery1 || guess2 == lottery2 || guess2 == lottery3
|| guess3 == lottery1 || guess3 == lottery2 || guess3 == lottery3){
System.out.println("\n\nCongratulations! You matched a lottery number.");
System.out.println("You won Php10,000!");
}else{
System.out.println("\n\nSorry, your lucky numbers didn't matched any of the lottery numbers!");
}
}`
public static void main(String[] args) {
Scanner lottery = new Scanner(System.in);
int lottery1, lottery2, lottery3;
int guess1, guess2, guess3;
System.out.println("\t\t\t\t\t\t~Welcome to THE LOTTERY~");
System.out.println("Enter your first two-digit lucky number!");
guess1 = lottery.nextInt();
System.out.println("Enter your second two-digit lucky number!");
guess2 = lottery.nextInt();
System.out.println("Enter your last two-digit lucky number!");
guess3 = lottery.nextInt();
System.out.println("The winning numbers are: ");
System.out.printf("%02d", lottery1 = (int)(Math.random() * 100));
System.out.printf("\n" + "%02d", lottery2 = (int)(Math.random() * 100));
System.out.printf("\n" + "%02d", lottery3 = (int)(Math.random() * 100));
if(guess1 == lottery1 && guess2 == lottery2 && guess3 == lottery3){
System.out.println("\n\nCongratulations! You matched all the lottery numbers in order.\nYou won Php100, 000!");
}
else if(((guess1 == lottery1) || (guess1 == lottery2) || (guess1 == lottery3))
&& ((guess2 == lottery1) || (guess2 == lottery2) || (guess2 == lottery3))
&& ((guess3 == lottery1) || (guess3 == lottery2) || (guess3 == lottery3))){
System.out.println("\n\nCongratulations! You matched all the lottery numbers.\nYou won Php30, 000!");
}
else if(guess1 == lottery1 || guess1 == lottery2 || guess1 == lottery3
|| guess2 == lottery1 || guess2 == lottery2 || guess2 == lottery3
|| guess3 == lottery1 || guess3 == lottery2 || guess3 == lottery3){
System.out.println("\n\nCongratulations! You matched a lottery number.\nYou won Php10, 000!");
}else{
System.out.println("\n\nSorry, your lucky numbers didn't matched any of the lottery numbers!");
}
}