-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGuessingGame.java
More file actions
34 lines (30 loc) · 1.18 KB
/
Copy pathGuessingGame.java
File metadata and controls
34 lines (30 loc) · 1.18 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
import java.util.Scanner;
public class GuessingGame {
public static void main(String[] args) {
int computerNumber = (int) (Math.random()*100 + 1);
System.out.println("A random number has been generated...");
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a guess between 1 and 100");
int userAnswer = scanner.nextInt();
for (int tries=0; tries<3; tries++) {
// Guess 'tries'
if (userAnswer <=0 || userAnswer >100) {
System.out.println("Invalid response");
}
else if (userAnswer == computerNumber ){
System.out.println("Correct! You win! See you next time!");
System.exit(0);
}
else if (userAnswer > computerNumber) {
System.out.println("Your guess is too high, guess again.");
}
else if (userAnswer < computerNumber) {
System.out.println("Your guess is too low, guess again.");
}
else {
System.out.println("Your guess is incorrect");
}
}
System.out.println("Sorry for your luck, restart the program to play again!");
}
}