File tree Expand file tree Collapse file tree 2 files changed +35
-0
lines changed
Exercise_21/Exercise_21_15 Expand file tree Collapse file tree 2 files changed +35
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments