Skip to content

Instantly share code, notes, and snippets.

@gitaficionado
Created November 6, 2019 05:40
Show Gist options
  • Save gitaficionado/8d883772ef49af4153d7ad74c7dfd6b8 to your computer and use it in GitHub Desktop.
Save gitaficionado/8d883772ef49af4153d7ad74c7dfd6b8 to your computer and use it in GitHub Desktop.
(Game: lottery) Revise Listing 3.8, Lottery.java, to generate a lottery of a two-digit number. The two digits in the number are distinct. (Hint: Generate the first digit. Use a loop to continuously generate the second digit until it is different from the first digit.
public class LotteryWithTwoDigits_5_32 {
public static void main(String[] args) {
// Generate the first digit
__________________ = ____________________
// Generate the second digit
int second = (int)(Math.random() * 10);;
while (first == _________) {
second = (int)(Math.random() * 10);
}
// Prompt the user to enter a guess
java.util.Scanner input = new java.util.Scanner(System.in);
System.out.print("Enter your two digits lottery pick: ");
int guess = input.nextInt();
// Check the guess
if (guess / 10 == first && guess % 10 == second) {
System.out.println("Exact match: you win $10,000");
}
else if (guess % 10 == first && guess / 10 == second) {
System.out.println("Match all digits: you win $3,000");
}
else if (guess % 10 == first || guess % 10 == second
|| guess / 10 == first || guess / 10 == second) {
System.out.println("Match one digit: you win $1,000");
}
else {
System.out.println("Sorry, no match");
}
System.out.println("Lottery is " + first + second);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment