-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1ea55ac
commit c214fc3
Showing
1 changed file
with
34 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,14 +2,44 @@ | |
* @author : Jonathan Birkey | ||
* @mailto : [email protected] | ||
* @created : 28Feb2024 | ||
* <p>(Display leap years) Write a program that displays all the leap years, 10 per line, from | ||
* 101 to 2100, separated by exactly one space. Also display the number of leap years in this | ||
* period. | ||
* <p>(Game: lottery) Revise Listing 3.8, Lottery.java, to generate a lottery of a two-digit | ||
* number. The two digits in the number are distinct. (Hint: Generate the first digit. Use a | ||
* loop to continuously generate the second digit until it is different from the first digit.) | ||
*/ | ||
package com.github.jonathanbirkey.chapter05; | ||
|
||
import java.util.Scanner; | ||
|
||
public class Exercise32 { | ||
public static void main(String[] args) { | ||
// TODO: solve | ||
int lotteryDigit1 = (int) (Math.random() * 10); | ||
int lotteryDigit2 = 0; | ||
do { | ||
lotteryDigit2 = (int) (Math.random() * 10); | ||
}while(lotteryDigit1 == lotteryDigit2); | ||
|
||
|
||
Scanner input = new Scanner(System.in); | ||
System.out.print("Enter your lotter pick (two digits): "); | ||
int guess = input.nextInt(); | ||
input.close(); | ||
|
||
int guessDigit1 = guess / 10; | ||
int guessDigit2 = guess % 10; | ||
|
||
System.out.printf("The lottery number is %d%d\n", lotteryDigit1, lotteryDigit2); | ||
|
||
if (guessDigit1 == lotteryDigit1 && guessDigit2 == lotteryDigit2) { | ||
System.out.println("Exact match: you win $10,000"); | ||
} else if (guessDigit2 == lotteryDigit1 && guessDigit1 == lotteryDigit2) { | ||
System.out.println("Match all digits: you win $3,000"); | ||
} else if (guessDigit1 == lotteryDigit1 | ||
|| guessDigit1 == lotteryDigit2 | ||
|| guessDigit2 == lotteryDigit1 | ||
|| guessDigit2 == lotteryDigit2) { | ||
System.out.println("Match one digit: you win $1,000"); | ||
} else { | ||
System.out.println("Sorry, no match"); | ||
} | ||
} | ||
} |