-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNumberGuessing.java
33 lines (31 loc) · 952 Bytes
/
NumberGuessing.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
32
33
import java.util.*;
class NumberGuessing {
public static void main(String[] args) {
Random rand=new Random();
int randNumber=rand.nextInt(100)+1;
int num;
int count=0;
boolean flag=false;
Scanner sc=new Scanner(System.in);
while(true){
System.out.println("Guess a number between (1-100): ");
num=sc.nextInt();
count++;
if (num==randNumber){
flag=true;
System.out.println("Your guessing was correct ");
System.out.println("You guessed in "+ count+" attempts ");
break;
}
else if (num>randNumber){
System.out.println("Guess lower ");
System.out.println();
}
else{
System.out.println("Guess Higher ");
System.out.println();
}
}
}
}
// Code by Monica