-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathHighLow.java
33 lines (30 loc) · 808 Bytes
/
HighLow.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
package jl223vy_assign2;
import java.util.*;
public class HighLow {
public static void main(String[] args){
Random rd = new Random();
Scanner sc = new Scanner(System.in);
int n=rd.nextInt(100)+1; //random integer between [1,100]
for(int i=1; i<=11; i++){
if(i==11){ //exit if tried more than 10 times
System.out.println("\nSorry!\nYou have tried 10 times!\nThe correct number is: "+n);
System.exit(0);
}
System.out.print("Guess "+ i +": ");
int guess=sc.nextInt();
if(guess>n){
System.out.println(" Clue: lower");
continue;
}
else if(guess<n){
System.out.println(" Clue: higher");
continue;
}
else{
System.out.println(" Correct answer after only "+ i +" guesses ¨C Excellent!");
break;
}
}
sc.close();
}
}