|
2 | 2 | * @author : Jonathan Birkey
|
3 | 3 |
|
4 | 4 | * @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.) |
8 | 8 | */
|
9 | 9 | package com.github.jonathanbirkey.chapter05;
|
10 | 10 |
|
| 11 | +import java.util.Scanner; |
| 12 | + |
11 | 13 | public class Exercise32 {
|
12 | 14 | 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 | + } |
14 | 44 | }
|
15 | 45 | }
|
0 commit comments