-
Notifications
You must be signed in to change notification settings - Fork 0
/
GuessTheNumber.java
31 lines (30 loc) · 1.16 KB
/
GuessTheNumber.java
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
import java.util.Scanner;
public class GuessTheNumber {
public static void main(String[] args){
int num = (int)(Math.random()*100);
Scanner sc = new Scanner(System.in);
int guessNumber = Integer.MIN_VALUE;
int count = 1;
System.out.println("You have to guess the number, Lets see in how many moves you guess the right number. If you want to exit, type any negative number");
while(true){
System.out.println("Enter the Number");
guessNumber = sc.nextInt();
if(guessNumber==num){
System.out.println("You guessed the number !! Wooohooooo, in " + count + " turns");
break;
}else if(guessNumber < 0){
System.out.println("Thanks for playing");
break;
}
else if(guessNumber > num){
System.out.println("The number you guessed is higher.");
count++;
}else{
System.out.println("The number you guessed is smaller.");
count++;
}
System.out.println("----------------------");
}
sc.close();
}
}