-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGuessingGame.java
More file actions
42 lines (36 loc) · 1.19 KB
/
Copy pathGuessingGame.java
File metadata and controls
42 lines (36 loc) · 1.19 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
36
37
38
39
40
41
import java.util.Scanner;
public class GuessingGame {
int random;
GuessingGame() {
random = (int) Math.ceil(Math.random() * 100);
}
/**
* @param guessNumber the number that player guessed
* @return
* - Negative if the guessed number is smaller
* - 0 if the guessed number is correct.
* - Positive if the guessed number is higher.
*/
int guess(int guessNumber) {
return guessNumber - random;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
GuessingGame game = new GuessingGame();
System.out.println("Welcome to the guessing game. Guess the number between 1-100");
int guess;
int result;
do {
System.out.print("Guess the number: ");
guess = input.nextInt();
result = game.guess(guess);
if (result == 0) {
System.out.println("Congrats, your guess is correct");
} else if (result < 0) {
System.out.println("Please Guess Higher");
} else {
System.out.println("Please Guess Lower");
}
} while (result != 0);
}
}