In: Computer Science
The program will first prompt the user for a range of numbers. Then the game will generate a random number in that range. The program will then allow the user to guess 3 times. Each time the person takes a guess, if it is not the number then the game should tell them to guess higher or lower. They only get 3 guesses. If they have not guessed the number in three tries then the game should tell them the computer won and thank you for playing and ask them if they would like to play again. If the user wins then the game should congratulate them and ask them if they would like to play again. Each time the program repeats you should store the random number generated into an array. When the program is done the program should display the all of the random numbers back to the user.
In java language
Hi,
Please find the below Output:
Please find the below code:
import java.util.Scanner;
import java.util.ArrayList;
public class Main
{
public static void main(String[] args) {
//Declare the array which will store the Random numbers
ArrayList array = new ArrayList(100);
//pass the array to our method to start the execution
processLogic(array);
}
//This will acept the array parameter to store the random generated
public static void processLogic(ArrayList array){
//Declare Global variables
Scanner scan;
String message;
//show message tot user that to generate the random number
message = "Please enter the range ro generate the random number";
System.out.println(message);
//Get the range number under which random number should generate
scan = new Scanner(System.in);
int inputnumber = scan.nextInt();
//pass the range to the function so it will generate the random number within range 1 - range number
int range = getRandomNumber(1,inputnumber);
//Add the generated random number in array
array.add(range);
//declare total guess number and valid answer flag
int iTotalGuess = 3; int iValidAnswer = 1;
//check 3 guesses for end user
for(int i=0;i<iTotalGuess; i++){
int iTry =i+1;
message = "Please Guess your "+ iTry +" try";
System.out.println(message);
//get the try from end user
scan = new Scanner(System.in);
int guessNumber = scan.nextInt();
//check the enered number from user and random number are equal or not
if(guessNumber == range){
iValidAnswer = 1;
break;
}
else if(guessNumber > range){
iValidAnswer = 0;
System.out.println("Please guess the lower number");
}
else if(guessNumber < range){
iValidAnswer = 0;
System.out.println("Please guess the heigher number");
}
}
//show the message based on users guess
if(iValidAnswer > 0){
System.out.println("Congratulation!! You WON!!");
}else{
System.out.println("Computer won and thank you for playing");
}
//ask user to play agin or not
String playAgainMsg = "Would you like to play again? Press 1 for Play again, other for quit";
System.out.println(playAgainMsg);
scan = new Scanner(System.in);
int playAgainNumber = scan.nextInt();
//if user enterd 1 then start another game
if(playAgainNumber == 1)
processLogic(array);
else{
//if user enteered other than 1 then show all the random number generated
System.out.println("Belwo are the Random Numbers:");
for(int i = 0; i < array.size(); i++) {
System.out.println(array.get(i));
}
}
}
public static int getRandomNumber(int min, int max) {
return (int) ((Math.random() * (max - min)) + min);
}
}
Thanks.