forked from kishanrajput23/Java-Projects-Collections
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGuess.java
97 lines (79 loc) · 2.49 KB
/
Guess.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import java.util.Scanner;
public class Guess
{
public static void main(String[] args) {
//creating Scanner object
Scanner in = new Scanner(System.in);
//Instructions to the Game
System.out.println("NUMBER GUESSING GAME");
System.out.println();
System.out.println("INSTRUCTIONS:");
System.out.println("1)Enter the minimum and maximum range of your choice");
System.out.println("2)Your Aim is to guess the random number generated between the above range");
System.out.println("3)If you find the random number in minimum number of guesses");
System.out.println("You WIN or you LOSE the game");
System.out.println();
System.out.println("OK LET'S BEGIN");
System.out.println();
//Declaring min and max variable for min and max range
int min;
int max;
int res;
int guess_count = 0;
//To get min and max value from the user
System.out.println("Enter your minimum range");
min = in.nextInt();
System.out.println("Enter your maximum range");
max = in.nextInt();
System.out.println();
//Creating random number out of the min and max range
int n = (int)(Math.random()*(max-min+1)+min);
int guess;
/*
//Determine the max guesses for the given range
res = (int)(max / 4) + (min / 4);
res = res/2;
System.out.println(res);
*/
//Loop for guessing the number by the user
System.out.println("Enter your guess (between '" + min +"' and '"+ max+ "' range)");
while(true){
//To input the guess
guess = in.nextInt();
//To compare the user entered number with the guess
//case 1
if(guess > n){
System.out.println("NUMBER is LESS than your GUESS");
guess_count++;
}
//case 2
if(guess < n){
System.out.println("NUMBER is GREATER than your GUESS");
guess_count++;
}
//case 3
if(guess == n){
System.out.println("GUESS is CORRECT");
//To Continue the Loop
/* System.out.println("Do you want to play again(Press 1 else Press 0)");
res = in.nextInt();
//To Break the Loop
if(!(res == 1)){
*/
break;
//}
}
System.out.println();
System.out.println("Enter your new GUESS");
}
System.out.println();
if(guess_count >= 5){
System.out.println("You LOST the game");
}
else{
System.out.println("You WON the game");
}
System.out.println();
System.out.println("GAME ENDED");
}
}