-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathguessTheNumber.java
More file actions
53 lines (40 loc) · 1.59 KB
/
guessTheNumber.java
File metadata and controls
53 lines (40 loc) · 1.59 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
42
43
44
45
46
47
48
49
50
51
52
53
// The mini project starts
import java.util.*;
// driver class
public class guessTheNumber {
public static void main(String[] args) {
// created a object of the Scanner class
Scanner sc = new Scanner(System.in);
// created a mynum variable which will have random int value
// every time the code is ran
int mynum = (int)(Math.random()*100);
// created and initialized a variable in which user input will be stored
int userNumber = 0;
// do-while loop to create a simple UI
do {
// taking the user input for the guess
System.out.print("Guess my number (1 to 100): ");
userNumber = sc.nextInt();
// if the guessed number is correct
if (userNumber == mynum) {
System.out.println("YEHHH!!! CORRECT NUMBER!!!");
break;
}
// if the guessed number is larger than THE number
else if (userNumber > mynum) {
System.out.println("Your number is too large!");
}
// if the guessed number is smaller than THE number
else if (userNumber < mynum) {
System.out.println("Your number is too small!");
}
// if the user have given a value less than 0 or more than 100
else {
System.out.println("WRONG INPUT!!!!");
}
}while(true);
// printing the The number that needed to be guessed
System.out.println("My Number was: " + mynum);
System.out.println("\n");
}
}