[英]Lottery Game using Math.random() in java
We are assigned to develop a program to play lottery.The program will randomly generates lottery of a two-digit numbers, and prompts the user to input a two-digit numbers.我们被指派开发一个程序来玩彩票。该程序会随机生成一个两位数的彩票,并提示用户输入一个两位数的号码。 And if the numbers is only a single digit(0-9) the numbers must be treated as a two-digit number (00 - 09).如果数字只是一位数 (0-9),则必须将数字视为两位数 (00 - 09)。 Below are the conditions:以下是条件:
This is my code so far...I actually have a problem on the 2nd condition (reward is Php30,000) whenever I matched only 1 number it says that "You matched all the lottery numbers. You won Php30,000."到目前为止,这是我的代码......我实际上在第二个条件(奖励是 Php30,000)上遇到了问题,每当我只匹配 1 个号码时,它就会说“你匹配了所有的彩票号码。你赢了 Php30,000”。
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!");
}
}
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.