-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberGame.java
More file actions
35 lines (29 loc) · 1.12 KB
/
NumberGame.java
File metadata and controls
35 lines (29 loc) · 1.12 KB
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
import java.util.Scanner;
public class NumberGame {
public static void Game() {
Scanner sc = new Scanner(System.in);
int number = 1 + (int) (100 * Math.random());
int K = 5;
int i, guess;
System.out.println("A number is chosen between 1 to 100. Guess the number within 5 trials.");
for (i = 0; i < K; i++) {
System.out.println("Guess the number:");
guess = sc.nextInt();
if (number == guess) {
System.out.println("Congratulations! You guessed the number.");
break;
} else if (number > guess && i != K - 1) {
System.out.println("The number is greater than " + guess);
} else if (number < guess && i != K - 1) {
System.out.println("The number is less than " + guess);
}
}
if (i == K) {
System.out.println("You have Completed your given trials.");
System.out.println("The number was " + number);
}
}
public static void main(String[] args) {
Game();
}
}