forked from League-Archive/IntroToJavaWorkshop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHiLowGame.java
More file actions
47 lines (38 loc) · 1.28 KB
/
HiLowGame.java
File metadata and controls
47 lines (38 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package day4;
import java.util.Random;
import javax.swing.JOptionPane;
public class HiLowGame {
public static void main(String[] args) {
// 3. Change this line to give you a random number between 1 - 100.
int Random = new Random().nextInt(1000);
// 2. Print out the random variable above
System.out.println(Random);
// 11. do the following 10 times
int question=-1;
for (int i = 0; i <10; i++) {
// 1. ask the user for a guess using a pop-up window, and save their response
String answer =JOptionPane.showInputDialog("guess a number between 0-1000");
// 4. convert the users’ answer to an int (Integer.parseInt(string))
question=(Integer.parseInt(answer));
// 5. if the guess is correct
if(question == Random){
// 6. win
JOptionPane.showMessageDialog(null,"Win ");
// 7. if the guess is high
JOptionPane.showMessageDialog(null, i+1 );
break;
}
if(question > Random){
// 8. tell them it's too high
JOptionPane.showMessageDialog(null,"nope ");
// 9. if the guess is low
}
if(question < Random){
// 10. tell them it's too low
JOptionPane.showMessageDialog(null,"nope ");
}}
// 12. tell them they lose
if(question != Random)
JOptionPane.showMessageDialog(null,"you lose ");
}
}