-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGuessTheRandomNumber.java
37 lines (28 loc) · 1.21 KB
/
GuessTheRandomNumber.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
35
36
37
import java.util.Scanner;
public class GuessTheRandomNumber {
public static void MiniProject(){
// Building a Mini Project
/*
The User will guess a pre decided random number. Either he will guess it correctly, or could enter -1 one to stop the game.
*/
Scanner sc3 = new Scanner(System.in);
int myNumber = (int)(Math.random()*100); // Since, random function generates a number between 0.01 to 0.99. therefore multiplied by 100
int userNumber = 0; // initialising userNumber Variable
do {
System.out.println("Guess My Number: ");
userNumber = sc3.nextInt();
if(userNumber == myNumber) {
System.out.println("WOOHOO .. CORRECT GUESS!!!");
break;
}
else if(userNumber > myNumber) {
System.out.println("your number is too large");
}
else {
System.out.println("your number is too small");
}
} while(userNumber >= 0);
System.out.print("My Number was: ");
System.out.println(myNumber);
}
}