In: Computer Science
Create a class called RandomGuess. In this game, generate and store a random number between 1 and 100. Then, continuously (in a loop):
import java.util.Random; import java.util.Scanner; public class RandomGuess { public static void main(String[] args) { Scanner in = new Scanner(System.in); Random random = new Random(); int number = random.nextInt(100) + 1; int tries = 0, guess; while (true) { System.out.print("Guess number: "); guess = in.nextInt(); tries++; if (guess == number) { break; } if (guess < number) { System.out.println("That is too low – Guess again"); } else { System.out.println("That is too high – Guess again"); } } System.out.println("Congrats! It took you " + tries + " tries!"); } }