In: Computer Science
Write a Java program that implements the Number Guessing Game:
1. First generate a random number (int) between 0 and 100, call it N
2. Read user input (a guess)
3. check the number, if it's smaller than N, output "The number is larger than that"
4. If the input is larger than N, output "The number is smaller than that"
5. If the input is equal to N, output " You got it!", and exit
6. Repeat until the user guesses the right number.
------- PART 2 : ---------------------
1. Modify the program to limit the number of user guesses to 7 (stop after 7 trials)
------- OPTIONAL : --------------------
2. Modify the program to change the role of the user and program (the user picks a number, and the program tries to guess what it is)
Part 1-
We will generate a random number between 1 to 100 using Math.random().
Now we will take inputs from the user and for each input we will check and print if the number guesses is greater or smaller than the given number.
If the guessed number is equal to the given number then the program will end with message "You got it".
Part 2-
In part 2 we have limited number of trials i.e 7.
Firstly we will generate a random number using Math.random().
Now we use a loop , which runs 7 times and print if the guesses number is greater or smaller then the given number.
If the guessed number is equal to the given number or if loop end without guessing the correct number ,then program will end with suitable message.
Optional part-
In this user will give a number and program will guess the number.
This was the approach for solving the problem.
I am giving you code for solving part 1 ,part 2 and optional part of problem by using a menu driven code which has two options -
1. Unlimited guesses(if user will choose 1 the program will work in the way given in part 1)
2. 7 guesses(if user choose 2 then the program will work in the way given in part 2)
3. If user choose 3 then the user will give the number and program will guess it as given in the optional part
We have completed part 1 , 2 and optional part in single program.
Here is the code-
import java.util.Scanner;
class NumberGame
{
static void unlimitedGuess() {
Scanner ms = new Scanner(System.in);
int n = (int)(Math.random()*101)+1;
boolean found = false;
while (!found)
{
System.out.println("Guess the number.");
int guess = ms.nextInt();
if (guess == n){
found = true;
System.out.println("You got it!");
break;
}
else if (guess > n)
System.out.println("The number is smaller than that.");
else
System.out.println("The number is larger than that.");
}
if (found)
System.out.println("Congratulations!");
else
System.out.println("Bad luck!");
}
static void sevenGuesses() {
Scanner ms = new Scanner(System.in);
int n = (int)(Math.random()*101)+1;
int count = 7;
boolean found = false;
while (count>0)
{
System.out.println("Guess the number. ("+count+" chances remaining)");
count--;
int guess = ms.nextInt();
if (guess == n){
found = true;
System.out.println("You got it!");
break;
}
else if (guess > n)
System.out.println("The number is smaller than that.");
else
System.out.println("The number is larger than that.");
}
if (found)
System.out.println("Congratulations!");
else
System.out.println("Bad luck!");
}
static void systemGuess() {
Scanner ms = new Scanner(System.in);
System.out.println("Write any number and I'll guess");
int n = ms.nextInt();
int prevg = -1, prevl = 1000;
while(true) {
int a = (int)(Math.random()*101)+1;
while (a<prevl || a>prevg)
a = (int)(Math.random()*101)+1;
if (a > n) {
System.out.println("I guessed "+a+" which is greater than "+n);
prevg = a;
}
else if (a < n)
{
System.out.println("I guessed "+a+" which is lesser than "+n);
prevl = a;
}
else {
System.out.println("I got this!. "+a);
break;
}
}
}
public static void main(String[] args)
{
Scanner ms = new Scanner(System.in);
System.out.println("Welcome to the guessing game!");
System.out.println("How you want to play?");
System.out.println("\t1.)Unlimited Guesses.\n\t2.)7 Guesses\n\t3.)Want me to guess?");
int a = ms.nextInt();
if (a == 1)
unlimitedGuess();
else if (a == 2)
sevenGuesses();
else if (a == 3)
systemGuess();
}
}
Sample output of program-