Code: import java.util.Random; import java.util.Scanner; public class Main { public static void main(String[] args) { Random rand = new Random(); int random_number = rand.nextInt(100); System.out.println("random int between 1 to 100: " + random_number); Scanner input = new Scanner(System.in); System.out.println("Enter number between 1 and 4: "); int number = input.nextInt(); if (random_number % number == 0) { System.out.print(number +" is a factor of "+ random_number); } else { System.out.print(number +" is not a factor of "+ random_number); } } }
Added by William M.
Close
Step 1
The code imports two classes from the `java.util` package: `Random` and `Scanner`. These classes are used to generate random numbers and read user input, respectively. Show more…
Show all steps
Your feedback will help us improve your experience
Reynald Oliveria and 74 other AP CS educators are ready to help you.
Ask a new question
Labs
Want to see this concept in action?
Explore this concept interactively to see how it behaves as you change inputs.
Key Concepts
Recommended Videos
make a program that generates a random number 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.
Frank D.
Supreeta N.
17. Random Number Guessing Game Write a program 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. Your program should keep a count of the number of guesses the user makes. When the user correctly guesses the random number, the program should display the number of guesses along with the message of congratulations. For testing purposes, display the random number right after it gets generated. (JAVA)
Gio M.
Recommended Textbooks
Computer Science and Information Technology
Introduction to Programming Using Python
Computer Science - An Overview
Watch the video solution with this free unlock.
EMAIL
PASSWORD