-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGuessTheNumber.java
More file actions
45 lines (39 loc) · 1.59 KB
/
GuessTheNumber.java
File metadata and controls
45 lines (39 loc) · 1.59 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
package randomnumber;
import java.util.Scanner;
import java.util.Random;
public class GuessTheNumber {
public static void main(String[] args) {
boolean playGame = true;
while(playGame == true) { // loops the program if the user inputs yes to replay
Scanner kb = new Scanner(System.in); // creates the scanner for user inputs
Random random = new Random(); // creates the random generator
int number = random.nextInt(100) + 1; // generates a random number within 100
System.out.println("Guess my number ");
int userNumber = kb.nextInt(); // the user guesses the random number
while(userNumber != number) { // loops the user until the guess the number correctly
if(userNumber > number) { // executes if the userNumber is bigger than the random number
System.out.println("Nope, try again. The number is smaller");
userNumber = kb.nextInt();
}
else if(userNumber < number) { // executes if the userNumber is smaller than the random number
System.out.println("Nope, try again. The number is bigger");
userNumber = kb.nextInt();
}
if(userNumber == number) { // decision to reset the game or end the program
System.out.println("CORRECT!");
System.out.println("Do you want to play again?");
String userOption = kb.next();
if(userOption.equalsIgnoreCase("yes")) { // resets the game if user says yes
System.out.println("Restarting...");
continue;
}
else { // ends the program if user doesn't say yes
System.out.println("The End");
playGame = false;
kb.close();
}
}
}
}
}
}