Java Reference
In-Depth Information
int
lottery = (
int
)(Math.random() *
100
);
6
7
8
// Prompt the user to enter a guess
9 Scanner input =
new
Scanner(System.in);
10 System.out.print(
"Enter your lottery pick (two digits): "
);
11
12
13
generate a lottery number
int
guess = input.nextInt();
enter a guess
// Get digits from lottery
14
int
lotteryDigit1 = lottery /
10
;
15
int
lotteryDigit2 = lottery %
10
;
16
17
// Get digits from guess
18
int
guessDigit1 = guess /
10
;
19
int
guessDigit2 = guess %
10
;
20
21 System.out.println(
"The lottery number is "
+ lottery);
22
23
// Check the guess
if
(guess == lottery)
24
25 System.out.println(
"Exact match: you win $10,000"
);
26
27
28 System.out.println(
"Match all digits: you win $3,000"
);
29
30
31
32
33 System.out.println(
"Match one digit: you win $1,000"
);
34
else
35 System.out.println(
"Sorry, no match"
);
36 }
37 }
exact match?
else if
(guessDigit2 == lotteryDigit1
match all digits?
&& guessDigit1 == lotteryDigit2)
else if
(guessDigit1 == lotteryDigit1
match one digit?
|| guessDigit1 == lotteryDigit2
|| guessDigit2 == lotteryDigit1
|| guessDigit2 == lotteryDigit2)
Enter your lottery pick (two digits):
The lottery number is 12
Sorry, no match
45
23
Enter your lottery pick:
The lottery number is 34
Match one digit: you win $1,000
line#
6
11
14
15
18
19
33
variable
lottery
34
guess
23
lotteryDigit1
3
lotteryDigit2
4
guessDigit1
2
guessDigit2
3
Output
Match one digit:
you win $1,000