-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGuessingNumberGame.java
34 lines (28 loc) · 1.1 KB
/
GuessingNumberGame.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
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();
// program your solution here. Do not touch the above lines!
int numberGuessed = 102;
int numberAttempts = 0;
while (numberGuessed != numberDrawn){
System.out.println("Guess a number: ");
numberGuessed = Integer.parseInt(reader.nextLine());
if (numberGuessed == numberDrawn) {
System.out.println("Congratulations, your guess is correct!");
} else if (numberGuessed < numberDrawn){
numberAttempts++;
System.out.println("The number is greater, guesses made: " + numberAttempts);
} else {
numberAttempts++;
System.out.println("The number is lesser, guesses made: " + numberAttempts);
}
}
}
// DO NOT MODIFY THIS!
private static int drawNumber() {
return new Random().nextInt(101);
}
}