Question

In: Computer Science

Exercise 1 JAVA LANGUAGE The system generates a random number from a given range, say 1...

Exercise 1 JAVA LANGUAGE

  • The system generates a random number from a given range, say 1 to 100.
  • The user is prompted to enter their given number in a displayed dialogue box.
  • The computer then tells if the entered number matches the guesses number or it is higher/lower than the generated number.
  • The game continues under the user guessing the number.
  • Throw an exception if user does enter an integer – have user reenter

Solutions

Expert Solution

Code:

//as we are using Scanner and random classes we have to
//import this package
import java.util.*;
//class name is Game,So the name of the file should be Game.java
class Game{
   public static void main(String[] args) {
       //creating instances for random and scanner
       Random rand = new Random();
       Scanner s = new Scanner(System.in);
       //variables declared
       int number,guess;
       //the generated random number is stored in variable number
       number= rand.nextInt(100);
       System.out.println("Guess the number:");
       //an infinite loop which only breaks when
       //user guesses the number
       while(true){
           //try block
           try{
               //taking user input
               guess=s.nextInt();
               if(guess<number){
                   System.out.println("The number you entered is lower than the generated number, Enter again");
               }
               else if(guess>number){
                   System.out.println("The number you entered is Higher than the generated number, Enter again");
               }
               //if the user guesses the number correctly, the loop breaks
               else{
                   System.out.println("Yay, You guessed the number correctly,The number is "+number);
                   break;
               }
               //exception is handled here
           }catch(InputMismatchException e){
               System.out.println(e);
               //asking the user to input again
               System.out.println("You didnt entered an Integer,Enter again: ");
               //for storing the new line character
               s.nextLine();
           };
       }
   }
}

Output:

Code Screenshot:

Code Snippet:

//as we are using Scanner and random classes we have to
//import this package
import java.util.*;
//class name is Game,So the name of the file should be Game.java
class Game{
        public static void main(String[] args) {
                //creating instances for random and scanner
                Random rand = new Random();
                Scanner s = new Scanner(System.in);
                //variables declared
                int number,guess;
                //the generated random number is stored in variable number
                number= rand.nextInt(100);
                System.out.println("Guess the number:");
                //an infinite loop which only breaks when
                //user guesses the number
                while(true){
                        //try block
                        try{
                                //taking user input
                                guess=s.nextInt();
                                if(guess<number){
                                        System.out.println("The number you entered is lower than the generated number, Enter again");
                                }
                                else if(guess>number){
                                        System.out.println("The number you entered is Higher than the generated number, Enter again");
                                }
                                //if the user guesses the number correctly, the loop breaks
                                else{
                                        System.out.println("Yay, You guessed the number correctly,The number is "+number);
                                        break;
                                }
                                //exception is handled here
                        }catch(InputMismatchException e){
                                System.out.println(e);
                                //asking the user to input again
                                System.out.println("You didnt entered an Integer,Enter again: ");
                                //for storing the new line character
                                s.nextLine();
                        };
                }
        }
}

Related Solutions

Write a program that generates a random number in the range of 1 through 100, and...
Write a program that generates a random number in the range of 1 through 100, and asks the user to guess what the number is. When the number is generated by the computer, don’t display the number to the user, but only display if the number generated is odd or even. If the user’s guess is higher than the random number, the program should display “Too high, try again.” If the user’s guess is lower than the random number, the...
Write a program that generates a random number in the range of 1 through 100, and...
Write a program that generates a random number in the range of 1 through 100, and asks the user to guess what the number is. When the number is generated by the computer, don’t display the number to the user, but only display if the number generated is odd or even. If the user’s guess is higher than the random number, the program should display “Too high, try again.” If the user’s guess is lower than the random number, the...
Write a program that generates a random number in the range of 1 through 100, and...
Write a program that generates a random number in the range of 1 through 100, and asks the user to guess what the number is. When the number is generated by the computer, don’t display the number to the user, but only display if the number generated is odd or even. If the user’s guess is higher than the random number, the program should display “Too high, try again.” If the user’s guess is lower than the random number, the...
In Java: Write a program that generates a random number and asks the user to guess...
In Java: Write a program that generates a random number and asks the user to guess the number and keeps track of how many guesses it took If the user input is negative or zero then the loop must stop reading further inputs and display how many guesses they used If they guess the correct number display a message telling them they got it and exit the program If they guess the wrong number (but still a legal guess) you...
Develop a C program that generates a random number from 1 to 100, then prompt the...
Develop a C program that generates a random number from 1 to 100, then prompt the user to guess the number until it is guessed correctly. Let the user know if the guess is too high, too low, or correct. The program should count the number of times the user guesses and display the number, along with their rank, after the number is guessed correctly. Rank the user as follows: Super Guesser: 1 to 4 guesses Excellent Guesser: 5 to...
Create a C++ program that generates a random number from 1 to 100 using the rand()...
Create a C++ program that generates a random number from 1 to 100 using the rand() function. Give the user a total 5 chances to guess the number. Stop the program and indicate the user guessed the correct number and should be applauded. Also, please find out which guess was the closest to the actual number. For example, if the rand() produces 57 and the user guessed 10, 80, 52, 33 and 44 in their respective turns then print out...
Random Number Guessing Game Write a program in C++ that generates a random number between 1...
Random Number Guessing Game Write a program in C++ that generates a random number between 1 and 100 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display “Too high. Try again.” If the user’s guess is lower than the random number, the program should display “Too low. Try again.” The program should use a loop that repeats until the user correctly guesses the random number....
Write a program that repeatedly generates a random integer in the range [1, 100], one integer...
Write a program that repeatedly generates a random integer in the range [1, 100], one integer at a time, and displays the generated numbers on the screen according to the following rules: If the second number generated is greater than the first number, they are displayed on the screen in the order of their input and while the next random number generated is greater than the previous one, the random number is displayed on the screen and the program continues....
Write a program that repeatedly generates a random integer in the range [1, 100], one integer...
Write a program that repeatedly generates a random integer in the range [1, 100], one integer at a time, and displays the generated numbers on the screen according to the following rules: If the second number generated is greater than the first number, they are displayed on the screen in the order of their input and while the next random number generated is greater than the previous one, the random number is displayed on the screen and the program continues....
Write a program that repeatedly generates a random integer in the range [1, 100], one integer...
Write a program that repeatedly generates a random integer in the range [1, 100], one integer at a time, and displays the generated numbers on the screen according to the following rules: If the second number generated is greater than the first number, they are displayed on the screen in the order of their input and while the next random number generated is greater than the previous one, the random number is displayed on the screen and the program continues....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT