Java Reference
In-Depth Information
35
else
36 System.out.println(
"Sorry, no match"
);
37 }
38 }
Enter your lottery pick (two digits): 00
The lottery number is 00
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
The program generates two random digits and concatenates them into the string
lottery
(lines 6-7). After this,
lottery
contains two random digits.
The program prompts the user to enter a guess as a two-digit string (line 12) and checks the
guess against the lottery number in this order:
First check whether the guess matches the lottery exactly (line 25).
■
If not, check whether the reversal of the guess matches the lottery (line 27).
■
If not, check whether one digit is in the lottery (lines 30-33).
■
If not, nothing matches and display “Sorry, no match” (line 36).
■
You can use the
System.out.printf
method to display formatted output on the
console.
Key
Point
Often, it is desirable to display numbers in a certain format. For example, the following code
computes interest, given the amount and the annual interest rate.
double
amount =
12618.98
;
double
interestRate =
0.0013
;
double
interest = amount * interestRate;
System.out.println(
"Interest is $"
+ interest);
Interest is $16.404674
Search WWH ::
Custom Search