Skip to content

Commit aab3d27

Browse files
author
jsquared21
committed
Add Ex 21.15
1 parent a241c47 commit aab3d27

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed
1.38 KB
Binary file not shown.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*********************************************************************************
2+
* (Addition quiz) Rewrite Programming Exercise 11.16 to store the answers in a *
3+
* set rather than a list. *
4+
*********************************************************************************/
5+
import java.util.*;
6+
7+
public class Exercise_21_15 {
8+
public static void main(String[] args) {
9+
int number1 = (int)(Math.random() * 10);
10+
int number2 = (int)(Math.random() * 10);
11+
12+
// Create a Scanner
13+
Scanner input = new Scanner(System.in);
14+
15+
// Create an set
16+
Set<Integer> answers = new HashSet<>();
17+
18+
System.out.print(
19+
"What is " + number1 + " + " + number2 + "? ");
20+
int answer = input.nextInt();
21+
22+
while (number1 + number2 != answer) {
23+
if (answers.contains(answer))
24+
System.out.println("You already entered " + answer);
25+
else {
26+
System.out.print("Wrong answer. Try again. What is "
27+
+ number1 + " + " + number2 + "? ");
28+
answers.add(answer);
29+
}
30+
answer = input.nextInt();
31+
}
32+
33+
System.out.println("You got it!");
34+
}
35+
}

0 commit comments

Comments
 (0)