-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathGuessingNumberGame.java
More file actions
31 lines (26 loc) · 930 Bytes
/
GuessingNumberGame.java
File metadata and controls
31 lines (26 loc) · 930 Bytes
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
import java.util.Random;
import java.util.Scanner;
public class GuessingNumberGame {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
int numberDrawn = drawNumber();
int i = 0;
while (true){
System.out.println("Guess a number: ");
int guess = Integer.parseInt(reader.nextLine());
if(guess == numberDrawn){
break;
} else if (guess < numberDrawn) {
i++;
System.out.println("Your number is greater, guesses made: " + i);
} else if (guess > numberDrawn) {
i++;
System.out.println("Your number is lesser, guesses made: " + i);
}
}
System.out.println("Congratulations, your guess is correct");
}
private static int drawNumber() {
return new Random().nextInt(101);
}
}