-
Notifications
You must be signed in to change notification settings - Fork 857
Expand file tree
/
Copy pathExercise_03_15.java
More file actions
executable file
·74 lines (65 loc) · 2.76 KB
/
Exercise_03_15.java
File metadata and controls
executable file
·74 lines (65 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
(Game: lottery) Revise Listing 3.8, Lottery.java, to generate a lottery of a threedigit
number. The program prompts the user to enter a three-digit number and
determines whether the user wins according to the following rules:
1. If the user input matches the lottery number in the exact order, the award is
$10,000.
2. If all digits in the user input match all digits in the lottery number, the award is
$3,000.
3. If one digit in the user input matches a digit in the lottery number, the award is
$1,000.
*/
import java.util.Scanner;
public class Exercise_03_15 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// Generate a random three-digit number
int lottery = (int)(Math.random() * 1000);
// Prompt the user to enter a three-digit number
System.out.print("Enter a three-digit number: ");
int guess = input.nextInt();
// Extract digits from lottery
int lotteryDigit1 = lottery / 100;
int remainingDigits = lottery % 100;
int lotteryDigit2 = remainingDigits / 10;
int lotteryDigit3 = remainingDigits % 10;
// Extract digits from guess
int guessDigit1 = guess / 100;
int remainingDigits = guess % 100;
int guessDigit2 = remainingDigits / 10;
int guessDigit3 = remainingDigits % 10;
System.out.println("The lottery number is " + lottery);
// Check the guess
if (guess == lottery)
System.out.println("Exact match: you win $10,000");
else if( guessDigit1 == lotteryDigit2 && guessDigit2 == lotteryDigit3 && guessDigit3 == lotteryDigit1 )
{
System.out.println("digits match, but different order: you win $1,000");
}
else if( guessDigit1 == lotteryDigit3 && guessDigit2 == lotteryDigit1 && guessDigit3 == lotteryDigit2 )
{
System.out.println("digits match, but different order: you win $3,000");
}
else if( guessDigit1 == lotteryDigit3 && guessDigit2 == lotteryDigit2 && guessDigit3 == lotteryDigit1 )
{
System.out.println("digits match, but different order: you win $3,000");
}
else if( guessDigit1 == lotteryDigit1 && guessDigit2 == lotteryDigit3 && guessDigit3 == lotteryDigit2 )
{
System.out.println("digits match, but different order: you win $3,000");
}
else if( guessDigit1 == lotteryDigit2 && guessDigit2 == lotteryDigit1 && guessDigit3 == lotteryDigit3 )
{
System.out.println("digits match, but different order: you win $3,000");
}
else if (guessDigit1 == lotteryDigit2 || guessDigit1 == lotteryDigit1 || guessDigit1 == lotteryDigit3 ||
guessDigit2 == lotteryDigit2 || guessDigit2 == lotteryDigit1
|| guessDigit2 == lotteryDigit3 || guessDigit3 == lotteryDigit2 || guessDigit3 == lotteryDigit1
|| guessDigit3 == lotteryDigit3)
{
System.out.println("One digit match: you win $1,000");
}
else
System.out.println("No match : Sorry you don't win anything.");
}
}