Skip to content

Instantly share code, notes, and snippets.

@gitaficionado
Created October 11, 2019 13:21
Show Gist options
  • Save gitaficionado/1bd743263885a7f39e85d581d1a0555b to your computer and use it in GitHub Desktop.
Save gitaficionado/1bd743263885a7f39e85d581d1a0555b to your computer and use it in GitHub Desktop.
(Game: lottery) Revise Listing 3.8, Lottery.java, to generate a lottery of a three-digit number. See page pg. 102 for more infromation .
public class Lottery_03_15 {
public static void main(String[] args) {
// Generate a lottery using Math.random here for 3 digits using a variable called lottery.
// Prompt the user to enter a guess by asking them to enter three digits. Variable must be called guess and be intialized as an it.
java.util.Scanner input = new java.util.Scanner(System.in);
// Get digits
int l1 = lottery / 100;
int l2 = (lottery % 100) / 10; // l2 = (lottery / 10) % 10
int l3 = lottery % 10;
int g1 = guess / 100;
int g2 = (guess % 100) / 10;
int g3 = guess % 10;
System.out.println("Lottery is " + lottery);
// Check the guess
if (___________ == __________)
System.out.println("Exact match: you win $10,000");
else if (g1 == l1 && g3 == l2 && g2 == l3 ||
g2 == l1 && g1 == l2 && g3 == l3 ||
g2 == l1 && g3 == l2 && g1 == l3 ||
g3 == l1 && g1 == l2 && g2 == l3 ||
g3 == l1 && g2 == l2 && g1 == l3)
System.out.println("Match all digits: you win $3,000");
else if (g1 == l1 || g1 == l2 || g1 == l3 ||
g2 == l1 || g2 == l2 || g2 == l3 ||
g3 == l1 || g3 == l2 || g3 == l3)
System.out.println("Match one digit: you win $1,000");
else
System.out.println("Sorry, no match");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment