Respuesta :
Answer:
The following are the program in the Java Programming Language.
import java.util.Random; //import package
import java.util.Scanner; //import package
//define a class
public class GuessNumber {
//define a main method
public static void main(String[] args)
{
//create object of the Scanner class
Scanner cs = new Scanner(System.in);
//print message
System.out.println("Guess the secret number\n");
//print message
System.out.print("Enter the maximum value for the secret number: ");
//get input in the integer variable through scanner class object
int maxNum=cs.nextInt();
//create object of the Random class
Random ran = new Random();
//declare an integer variable that store random number
int guessed = 1+ran.nextInt(maxNum);
//set the while loop
while (true)
{
//print message
System.out.println("\nA new secret number has been chosen.");
//set two integer variables to 0
int user = 0;
int count = 0;
//set the while loop
while (user != guessed)
{
//print message
System.out.print("Enter guess: ");
//get input in the variable from user
user = cs.nextInt();
//increment the variable by 1
count++;
//check that the user is less than guessed
if (user < guessed)
{
//then, print message
System.out.println("low, try again.");
}
//check that user is greater than guessed
else if (user > guessed)
{
//then, print message
System.out.println("high, try again");
}
}
//print message with count
System.out.println("You won in " + count + "!");
//print message and get input
System.out.print("\nPlay again? (Y/N)");
cs.nextLine();
String again = cs.nextLine();
//check that input is y
if(again.equalsIgnoreCase("y"))
{
//then, loop again iterates
continue;
}
//otherwise, loop is break
else{
break;
}
}
}
}
Explanation:
The following are the description of the program :
- Firstly, we import the required packages and define the class 'GuessNumber'.
- Inside the class, we create the object of the scanner class and the random class then, get input from the user through scanner class object and generate random number through the random class object.
- Then, set the while infinite loop in which we declare two integer data type variables and assign them to 0 then, set while loop inside the infinite loop that iterates when the 1st variable is not equal to the second one then, get input from the user through scanner class object.
- Then, check that input number is less than the random number then, print message otherwise, again check that input number is greater than the random number then, print message or if both numbers are equal then print the message with the count.
- Finally, the program asks for play again, if the input is 'y' then, infinite loop again iterates otherwise the infinite loop is break.
The java program to compile the numbers gotten from the user request is:
- import java.util.Scanner; //import package
- public class GuessNumber {
- public static void main(String[] args)
- {
- Scanner cs = new Scanner(System.in);
- System.out.println("Guess the secret number\n");
- System.out.print("Enter the maximum value for the secret number: ");
- int maxNum=cs.nextInt();
- Random ran = new Random();
- //declare an integer variable that store random number
- int guessed = 1+ran.nextInt(maxNum);
- //set the while loop
- while (true)
- {
- //print message
- System.out.println("\nA new secret number has been chosen.");
- //set two integer variables to 0
- int user = 0;
- int count = 0;
- //set the while loop
- while (user != guessed)
- {
- //print message
- System.out.print("Enter guess: ");
- //get input in the variable from user
- user = cs.nextInt();
- //increment the variable by 1
- count++;
- //check that the user is less than guessed
- if (user < guessed)
- {
- //then, print message
- System.out.println("low, try again.");
- }
- //check that user is greater than guessed
- else if (user > guessed)
- {
- //then, print message
- System.out.println("high, try again");
- }
- }
- //print message with coun
- System.out.println("You won in " + count + "!");
- //print message and get input
- System.out.print("\nPlay again? (Y/N)");
- cs.nextLine();
- String again = cs.nextLine();
- //check that input is y
- if(again.equalsIgnoreCase("y"))
- {
- //then, loop again iterates
- continue;
- }
- //otherwise, loop is break
- else{
- break;
- }
- }
- }
- }
Read more about java programming here:
https://brainly.com/question/18554491