1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
hichkok12 [17]
3 years ago
8

(JAVA programming) Guess Number program

Computers and Technology
1 answer:
amm18123 years ago
4 0

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:

<u>The following are the description of the program</u> :

  • 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.
You might be interested in
The email_list function receives a dictionary, which contains domain names as keys, and a list of users as values. Fill in the b
zysi [14]

Answer:

Following are the solution to this question:

Please find the attachment of this code.

Explanation:

In this code, an email_list method is declared, that accepts a domains parameter, and in the next step, an empty list and two for loop are defined, in which the first for loop is used for a count parameter value and in the second loop is use the append method to add the given value, and use the return method to print its value, and use the print method to call the "email_list" method.  

3 0
3 years ago
what will be the Trade discount and Invoice Amount if the List Price is $400.00 and the Trade Discount Rate is 85%
V125BC [204]

Answer:

$340

Explanation:

you have to multiply $400 bu 85% to get $340

4 0
3 years ago
MaKe TeA In ThE AnSwErs
AfilCa [17]
Pineapples don’t taste good.
4 0
3 years ago
After how many months should a tractor's steering fluid be changed?
matrenka [14]

Answer:

after every four months

Explanation:

3 0
3 years ago
4. To combine a range of cells into one large cell, you should highlight the cells and choose
Pachacha [2.7K]
Highlight the cells and choose merge cells
8 0
4 years ago
Other questions:
  • Which computer device works like the human central nervous system by connecting all the computer’s parts together and allowing t
    9·1 answer
  • ____________ is using digital media safely, responsibly, and ethically.
    12·2 answers
  • Why is it important to explore an Integrated
    13·1 answer
  • What type of computer is likely to use so-dimms, have an internal power supply, and use a desktop processor socket?
    10·1 answer
  • Universal Containers wants line items to be organized into pre-defined groups on the output document. The name of these pre-defi
    6·1 answer
  • Karel challenges answers
    11·1 answer
  • Mr. Ray is looking for a platform to launch his product. His marketing team advises him to launch the product via as it is free
    15·2 answers
  • A restaurant recorded the ages of customers on two separate days. You are going to write a program to find the minimum age of a
    5·2 answers
  • Who is the father of computer​
    7·1 answer
  • What is system software?
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!