THINK FIRST·CODE LATER

← All labs

Guess the number

Problem

Write GuessGame. The first input is the secret number (in a real game it would be random); then come the player's guesses. For each guess print 40 is too low or 40 is too high. Stop when the guess is correct, after 7 tries, or when there is no more input:

50 is too high
25 is too low
Correct! Found in 3 tries

or Game over. The number was 37 (write 1 try when found at once). Use a while loop with a boolean flag, and in.hasNextInt().

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

l7-3-data-1.txt

37 50 25 37
What your program must print for l7-3-data-1.txt
50 is too high
25 is too low
Correct! Found in 3 tries

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < l7-3-data-1.txt

Windows PowerShell does not understand <:

Get-Content l7-3-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.