Skip to content

Commit c214fc3

Browse files
finished exercise 32
1 parent 1ea55ac commit c214fc3

File tree

1 file changed

+34
-4
lines changed

1 file changed

+34
-4
lines changed

src/com/github/jonathanbirkey/chapter05/Exercise32.java

+34-4
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,44 @@
22
* @author : Jonathan Birkey
33
* @mailto : [email protected]
44
* @created : 28Feb2024
5-
* <p>(Display leap years) Write a program that displays all the leap years, 10 per line, from
6-
* 101 to 2100, separated by exactly one space. Also display the number of leap years in this
7-
* period.
5+
* <p>(Game: lottery) Revise Listing 3.8, Lottery.java, to generate a lottery of a two-digit
6+
* number. The two digits in the number are distinct. (Hint: Generate the first digit. Use a
7+
* loop to continuously generate the second digit until it is different from the first digit.)
88
*/
99
package com.github.jonathanbirkey.chapter05;
1010

11+
import java.util.Scanner;
12+
1113
public class Exercise32 {
1214
public static void main(String[] args) {
13-
// TODO: solve
15+
int lotteryDigit1 = (int) (Math.random() * 10);
16+
int lotteryDigit2 = 0;
17+
do {
18+
lotteryDigit2 = (int) (Math.random() * 10);
19+
}while(lotteryDigit1 == lotteryDigit2);
20+
21+
22+
Scanner input = new Scanner(System.in);
23+
System.out.print("Enter your lotter pick (two digits): ");
24+
int guess = input.nextInt();
25+
input.close();
26+
27+
int guessDigit1 = guess / 10;
28+
int guessDigit2 = guess % 10;
29+
30+
System.out.printf("The lottery number is %d%d\n", lotteryDigit1, lotteryDigit2);
31+
32+
if (guessDigit1 == lotteryDigit1 && guessDigit2 == lotteryDigit2) {
33+
System.out.println("Exact match: you win $10,000");
34+
} else if (guessDigit2 == lotteryDigit1 && guessDigit1 == lotteryDigit2) {
35+
System.out.println("Match all digits: you win $3,000");
36+
} else if (guessDigit1 == lotteryDigit1
37+
|| guessDigit1 == lotteryDigit2
38+
|| guessDigit2 == lotteryDigit1
39+
|| guessDigit2 == lotteryDigit2) {
40+
System.out.println("Match one digit: you win $1,000");
41+
} else {
42+
System.out.println("Sorry, no match");
43+
}
1444
}
1545
}

0 commit comments

Comments
 (0)