-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGuessNumber.java
36 lines (30 loc) · 1.19 KB
/
GuessNumber.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
package com.company;
import java.util.Scanner;
public class GuessNumber {
static Scanner sc = new Scanner(System.in);
static boolean hasWon = false;
public static void main(String[] args) {
int randomNumber = (int) (Math.random() * 100)+1;
System.out.println("I have randomly chosen a number between 1 and 100");
System.out.println("Try to guess it: ");
for (int i=10;i>0;i--){
System.out.println("You have " + i + " guess(es) left. Choose again");
int guess = sc.nextInt();
if (randomNumber < guess){
System.out.println("It's smaller than " + guess + " guess.");
}else if (randomNumber > guess){
System.out.println("It's higher than " + guess + " guess.");
} else {
hasWon = true;
break; //exit the entire for loop
}
//System.out.println("Your guess was: " + guess);
} //end of for loop
if (hasWon){
System.out.println("You guess it! you WIN!");
} else {
System.out.println("GAME OVER!");
System.out.println("The number was: " + randomNumber);
}
}
}