Question: Could someone pls help correct the following code below in java: import java.util.Random; import java.util.Scanner; //This is GuessingNumberGame.java class public class HiLo { //main method
Could someone pls help correct the following code below in java:
import java.util.Random;
import java.util.Scanner;
//This is GuessingNumberGame.java class
public class HiLo {
//main method
public static void main(String[] args) {
//Local variable of main method
int guess = -1; int numOfIteration=0;
//This is Scanner class object
Scanner input=new Scanner(System.in);
// This is Random class object, this is useful to generate random number
Random random = new Random();
//call the method for generating random number between 1 to 100
int number = random.nextInt(100)+1;
//this is while loop for iteration
//this loop iterated until user guess correct number
while (guess!=number) {
// display message for user
System.out.print("Guess the number between [1-100] :: ");
//store user guess number
guess = input.nextInt();
// Checking the guess is high, low or correct
if (guess==number) {
//display output
System.out.println("Your guess "+guess+" is correct");
System.out.println("You guess this number after "+numOfIteration+" tries!");
//asking user decision after correct guess
System.out.println("Do you want to play this game [y/n]");
char ch=input.next().charAt(0);
//if user want to continue this game execute if condition
if(ch=='y') {
//set numOfIteration variable is zero because this game is again started
numOfIteration=0;
continue;
}//if
else break;
} //if if (guess < 1 || guess > 100)
System.out.println("Guess should be between 1 and 100! Try again!");
else if (guess>number)
// Guess is too high
System.out.println("Your guessed number is too high, please try again");
else
// Guess is too low
System.out.println("Your guessed number is too low, please try again");
//increase if user doesn't guess correct number
numOfIteration++; // Display appropriate message if user guess is out of range
}
}//while loop
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
