-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberGuessingGame.java
More file actions
41 lines (37 loc) · 1.29 KB
/
NumberGuessingGame.java
File metadata and controls
41 lines (37 loc) · 1.29 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 NumberGuessingGame {
int random;
NumberGuessingGame(){
random = (int) Math.ceil(Math.random() * 100);
}
/**
*
* the number that player guessed
* Negative if the guessed number is smaller
* 0 if the guessed number is correct
* positive if the guessed number is greater
*/
int guess(int guessNumber){
return guessNumber - random;
}
public static void main(String[] args) {
Scanner input =new Scanner(System.in);
NumberGuessingGame game=new NumberGuessingGame();
System.out.println("Welcome to the guessing game, guess the number between 1 to 100");
int guess;
int result;
do {
System.out.println("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 a greater number");
}else {
System.out.println("Please guess a smaller number");
}
}while(result != 0);
System.out.println("You Guessed the Correct Number");
}
}