-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathguess.java
36 lines (31 loc) · 1.02 KB
/
guess.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
34
35
36
import java.util.Scanner;
public class guess {
public static void main(String[] arg){
System.out.println("Guess the number challenge!");
System.out.println("You wil get only 10 tries.");
Scanner sc = new Scanner(System.in);
int guess = (int) (Math.random()*100);
boolean win = false;
for(int i=0; i<10; i++){
System.out.print("Try " + (i+1) + ": ");
int num = sc.nextInt();
if(num==guess){
System.out.println("You guessed it right!");
win = true;
break;
}
if(num>guess){
System.out.println("It is greater than the number. Try again!");
}
else if(num<guess){
System.out.println("It is less than the number. Try again!");
}
else{
System.out.println("Wrong input.");
}
}
if(!win){
System.out.println("You lost.");
}
}
}