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. The program will make guesses and the user will tell the program to guess higher or lower.

Respuesta :

ijeggs

Answer:

import java.util.Random;

import java.util.Scanner;

public class ANot {

   public static void main(String[] args) {

     Scanner in = new Scanner(System.in);

     Random rand = new Random();

     int randomNum = rand.nextInt(100)+1;

     System.out.println("ENTER A NUMBER TO PLAY");

     int userNum = in.nextInt();

     if(userNum<randomNum){

         System.out.println("Guess Higher");

     }

     else if(userNum>randomNum){

         System.out.println("Guess Lower");

     }

     else{

         System.out.println("CORRECT GUESS");

     }

   }

}

Explanation:

  1. Use the Random class in Java to generate a random number
  2. Save the random number in a variable
  3. Use the Scanner Class to prompt user to enter a number as a guess
  4. Save the user input in a variable
  5. Use if/elseif/else statements to compare the random number and the number entered by the user.