Java Reference
In-Depth Information
34
else
35 System.out.println(
"Sorry, no match"
);
36 }
37 }
Enter your lottery pick (two digits): 15
The lottery number is 15
Exact match: you win $10,000
Enter your lottery pick (two digits): 45
The lottery number is 54
Match all digits: you win $3,000
Enter your lottery pick: 23
The lottery number is 34
Match one digit: you win $1,000
Enter your lottery pick: 23
The lottery number is 14
Sorry: no match
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
The program generates a lottery using the
random()
method (line 6) and prompts the user
to enter a guess (line 11). Note that
guess % 10
obtains the last digit from
guess
and
guess
/ 10
obtains the first digit from
guess
, since
guess
is a two-digit number (lines 18-19).
The program checks the guess against the lottery number in this order:
1. First, check whether the guess matches the lottery exactly (line 24).
2. If not, check whether the reversal of the guess matches the lottery (lines 26-27).
3. If not, check whether one digit is in the lottery (lines 29-32).
4. If not, nothing matches and display
"Sorry, no match"
(lines 34-35).
Search WWH ::
Custom Search