here is my question
1. Write a Java program that randomly generates a five-digit lottery number and prompts the user to enter a five-digit number. Each digit in the number is in the range between 0~9. The program should determine which prize the user wins according to the following rule:
• The user wins the first prize if the user input matches all five digits in the lottery number in exact order.
• The user wins the second prize if the user input matches any four digits in the lottery number in exact positions.
• The user wins the third prize if the user input matches any three digits in the lottery number in its exact position.
• The user wins the fourth prize if the user input matches any two digits in the lottery number in its exact position.
• The user wins the fifth prize if the user input matches any one digit in the lottery number in its exact position.
here is my code. I have tried replacing the && statements with || and it always returns either case 1 or case default.
import java.util.Scanner;
import java.util.Random;
class Hw5 {
static int getPrize(int g1, int g2, int g3, int g4, int g5,
int u1, int u2, int u3, int u4, int u5) {
//code for determining the prize comparing (g1, g2, g3, g4, g5) to (u1, u2, u3, u4, u5)
if (u1 == g1 && u2 == g2 && u3 == g3 && u4 == g4 && u5 == g5)
return 1;
else if (u1 == g1 || u2 == g2 || u3 == g3 || u4 == g4 && u5 != g5)
return 2;
else if (u1 == g1 && u2 == g2 && u3 == g3)
return 3;
else if (u1 == g1 && u2 == g2)
return 4;
else if (u1 == g1)
return 5;
else
return 0;
}
public static void main(String [] args) {
Scanner in = new Scanner(System.in);
Random rand = new Random();
int g1, g2, g3, g4, g5; //5-digit number generated
int u1, u2, u3, u4, u5; //5-digit number entered by user
int prize;
g1 = rand.nextInt(10); //generate an integer between 0 and 9
g2 = rand.nextInt(10);
g3 = rand.nextInt(10);
g4 = rand.nextInt(10);
g5 = rand.nextInt(10);
System.out.print(g1);
g1 = rand.nextInt(10);
System.out.print(g2);
g2= rand.nextInt(10);
System.out.print(g3);
g3= rand.nextInt(10);
System.out.print(g4);
g4= rand.nextInt(10);
System.out.print(g5);
g5= rand.nextInt(10);
System.out.print("Enter the first digit(0-9):");
u1 = in.nextInt();
System.out.print("Enter the second digit(0-9):");
u2 = in.nextInt();
System.out.print("Enter the third digit(0-9):");
u3 = in.nextInt();
System.out.print("Enter the fourth digit(0-9):");
u4 = in.nextInt();
System.out.print("Enter the fifth digit(0-9):");
u5 = in.nextInt();
prize = getPrize(g1, g2, g3, g4, g5, u1, u2, u3, u4, u5);
switch (prize) {
case 1:
System.out.println("You have won the first prize!");
break;
case 2:
System.out.println("You have won the second prize!");
break;
case 3:
System.out.println("You have won the third prize!");
break;
case 4:
System.out.println("You have won the fourth prize!");
break;
case 5:
System.out.println("You have won the fifth prize!");
break;
default:
System.out.println("Sorry, you didn't win any prize!");
break;
}
}
}