-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGuessTheNumber.java
More file actions
63 lines (56 loc) · 2.47 KB
/
GuessTheNumber.java
File metadata and controls
63 lines (56 loc) · 2.47 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import java.util.Scanner;
import java.lang.Math;
/* This class is about a number guessing game with hints about the guess if it's high or low and also shows how many attempts it took the user to guess the right number and also hints and correct guess have colors !!!
*/
public class GuessTheNumber{
public static final String RESET = "\u001B[0m";
public static final String RED = "\u001B[91m";
public static final String GREEN = "\u001B[92m";
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
boolean playAgain = true;
// This while loop will be executed if user playes the game
while(playAgain){
int attempts = 0;
int userInput = 0;
int random = (int)(Math.random() * 100) + 1;
System.out.println("I'm thinking of a number between 1 and 100.");
// This do...while loop will be executed until the user guesses the right number
do{
System.out.print("Enter your guess : ");
if(sc.hasNextInt()){
userInput = sc.nextInt();
attempts++;
if(random > userInput){
System.out.println(RED + "Too low! Try again." + RESET);
}else if(random < userInput){
System.out.println(RED + "Too high! Try again." + RESET);
}else{
System.out.println(GREEN + "Correct! You got it in " + attempts + " attempts." + RESET);
}
} else {
System.out.println("Please enter a valid number!");
sc.next(); // consume invalid input
}
}while(random != userInput);
// Check if user wants to continue playing game or not
System.out.print("Do you want to play again? (y/n):");
char choice = sc.next().trim().toLowerCase().charAt(0);
switch(choice){
case 'Y':
case 'y':
playAgain = true;
break;
case 'n':
case 'N':
playAgain = false;
System.out.println(GREEN + "Thanks for playing!" + RESET);
break;
default:
System.out.println(RED + "Invalid choice! Exiting..." + RESET);
playAgain = false;
}
}
sc.close();
}
}