Question

Write a program in Java where a user of this program will play a game in...

Write a program in Java where a user of this program will play a game in which he/she needs to guess a target number, which is a number that the program has randomly picked in the range that the user chooses. The program will repeatedly prompt for the guessed number and provide a clue whether the guessed number is bigger or smaller than the target number, until the guessed number equals the target number.

Homework Answers

Answer #1

//code:


import java.util.Scanner; 

public class Main{ 

        // Function that implements the 
        // number guessing game 
        public static void      numberGuessingGame() 
        { 
                // Scanner Class 
                Scanner sc = new Scanner(System.in); 
        //range of randomly choosen numbers(lowrange and highrange)
        int lrange,hrange;
        System.out.println(" Enter 2 numbers that the target number should be in the range :  ");
         lrange=sc.nextInt();
         hrange=sc.nextInt();
                // Generate the numbers 
                int target_number = lrange + (int)(hrange* Math.random()); 

                

                int i, guess; 

                
                // Interate over until target guess
                for (i = 0; ; i++) { 

                        System.out.println( 
                                "Guess the number:"); 

                        // Take input for guessing 
                        guess = sc.nextInt(); 

                        // If the number is guessed 
                        if (target_number == guess) { 
                                System.out.println( 
                                        "Congratulations! You guessed the number."); 
                                break; 
                        } 
                        else if (target_number > guess) { 
                                System.out.println( 
                                        "The number is greater than " + guess); 
                        } 
                        else if (target_number < guess) { 
                                System.out.println( 
                                        "The number is  less than " + guess); 
                        } 
                } 

                
        } 

        // Driver Code 
        public static void main(String arg[]) 
        { 

                // Function Call 
        numberGuessingGame();
        } 
} 

//screenshot

---------------------------------------------------------------------

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a program that plays a number guessing game with a human user. The human user...
Write a program that plays a number guessing game with a human user. The human user will think of a number between 1 and 100, inclusive. Then the program has to guess what the user entered. keep track of the number of interaction it takes for the computer to guess the number. sample run: enter number to be guessed:88 output: you entered 88, and it took the program 3 iterations to guess.
Write a Python program that plays a number guessing game with a human user. The human...
Write a Python program that plays a number guessing game with a human user. The human user will think of a number between 1 and 100, inclusive. Then the program has to guess what the user entered. keep track of the number of interaction it takes for the computer to guess the number. sample run: enter number to be guessed:88 output: you entered 88, and it took the program 3 iterations to guess.
Write a script that plays a simple “guess the number” game with the user. It should...
Write a script that plays a simple “guess the number” game with the user. It should select a random integer in the range [0 - 10], ask the user to guess the value, and provide feedback of the form “too high”, “too low”, or “congratulations” as appropriate. After the correct value is guessed, the script should terminate.
Write a Java program to randomly create an array of 50 double values. Prompt the user...
Write a Java program to randomly create an array of 50 double values. Prompt the user to enter an index and prints the corresponding array value. Include exception handling that prevents the program from terminating if an out of range index is entered by the user. (HINT: The exception thrown will be ArrayIndexOutOfBounds)
Using Java, write a program that allows the user to play the Rock-Paper-Scissors game against the...
Using Java, write a program that allows the user to play the Rock-Paper-Scissors game against the computer through a user interface. The user will choose to throw Rock, Paper or Scissors and the computer will randomly select between the two. In the game, Rock beats Scissors, Scissors beats Paper, and Paper beats Rock. The program should then reveal the computer's choice and print a statement indicating if the user won, the computer won, or if it was a tie. Allow...
Write Java program Lab51.java which takes in a string from the user, converts it to an...
Write Java program Lab51.java which takes in a string from the user, converts it to an array of characters (char[] word) and calls the method: public static int countVowels(char[]) which returns the number of vowels in word. (You have to write countVowels(char[]) ).
Write a java program that repeatedly prompts the user to input a string starting with letters...
Write a java program that repeatedly prompts the user to input a string starting with letters from the English alphabet. The program must stop getting input when the user inputs the string “STOOOOP”. Then the program must display the words starting with each letter from the alphabet in a separate line (e.g. you need to make a new line after all words starting with a specific letter are finished.
Write code in JAVA Write a program that will output a right triangle based on user...
Write code in JAVA Write a program that will output a right triangle based on user specified input height (int) and specified input symbol (char).The first line will have one user-specified character, such as % or *. Each subsequent line will have one additional user-specified character until the number in the triangle's base reaches specified input height. Output a space after each user-specified character, including after the line's last user-specified character. Hint: Use a nested for loop. Ex:If the input...
A. Write a Java program that asks the user to enter “bus” or “subway” or “walk”....
A. Write a Java program that asks the user to enter “bus” or “subway” or “walk”. If the user enters “bus” display “$1.00”. If they enter “subway” display “$1.50 and if they enter “walk” display “Free”. B. Write a java program that creates the following two arrays: String[] candidates = {“S Jones”,”Justin Fairfax”,”Clark Duncan”}; int[] votes = {7345,4324,3211}; Write the code that will search the votes array for the candidate with the largest number of votes and prints the name...
JAVA Write a program that has two functions in which the user chooses which one to...
JAVA Write a program that has two functions in which the user chooses which one to perform; 1. reads in a CSV file of integers into an array and insert it into a binary search tree class 2. deserializes an object and inserts it into a binary search tree class the tree class must be in a separate class from the CSV file read and deserialize object load