Skip to content

Commit 726d4a5

Browse files
Merge pull request kishanrajput23#176 from Nokia-A6/main
Create Guess.java
2 parents 7517bf8 + 493388f commit 726d4a5

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed

Guess.java

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import java.util.Scanner;
2+
public class Guess
3+
{
4+
public static void main(String[] args) {
5+
6+
//creating Scanner object
7+
Scanner in = new Scanner(System.in);
8+
9+
//Instructions to the Game
10+
System.out.println("NUMBER GUESSING GAME");
11+
System.out.println();
12+
13+
System.out.println("INSTRUCTIONS:");
14+
15+
System.out.println("1)Enter the minimum and maximum range of your choice");
16+
System.out.println("2)Your Aim is to guess the random number generated between the above range");
17+
System.out.println("3)If you find the random number in minimum number of guesses");
18+
System.out.println("You WIN or you LOSE the game");
19+
System.out.println();
20+
21+
System.out.println("OK LET'S BEGIN");
22+
System.out.println();
23+
24+
//Declaring min and max variable for min and max range
25+
int min;
26+
int max;
27+
int res;
28+
int guess_count = 0;
29+
30+
//To get min and max value from the user
31+
System.out.println("Enter your minimum range");
32+
min = in.nextInt();
33+
System.out.println("Enter your maximum range");
34+
max = in.nextInt();
35+
System.out.println();
36+
37+
//Creating random number out of the min and max range
38+
int n = (int)(Math.random()*(max-min+1)+min);
39+
int guess;
40+
41+
/*
42+
//Determine the max guesses for the given range
43+
res = (int)(max / 4) + (min / 4);
44+
res = res/2;
45+
System.out.println(res);
46+
*/
47+
48+
//Loop for guessing the number by the user
49+
System.out.println("Enter your guess (between '" + min +"' and '"+ max+ "' range)");
50+
while(true){
51+
52+
//To input the guess
53+
guess = in.nextInt();
54+
55+
//To compare the user entered number with the guess
56+
57+
//case 1
58+
if(guess > n){
59+
System.out.println("NUMBER is LESS than your GUESS");
60+
guess_count++;
61+
}
62+
63+
//case 2
64+
if(guess < n){
65+
System.out.println("NUMBER is GREATER than your GUESS");
66+
guess_count++;
67+
}
68+
69+
//case 3
70+
if(guess == n){
71+
System.out.println("GUESS is CORRECT");
72+
73+
//To Continue the Loop
74+
/* System.out.println("Do you want to play again(Press 1 else Press 0)");
75+
res = in.nextInt();
76+
77+
//To Break the Loop
78+
if(!(res == 1)){
79+
*/
80+
break;
81+
//}
82+
}
83+
System.out.println();
84+
System.out.println("Enter your new GUESS");
85+
}
86+
87+
System.out.println();
88+
if(guess_count >= 5){
89+
System.out.println("You LOST the game");
90+
}
91+
else{
92+
System.out.println("You WON the game");
93+
}
94+
System.out.println();
95+
System.out.println("GAME ENDED");
96+
}
97+
}

0 commit comments

Comments
 (0)