Assigned: Thursday, June 1
Due: Tuesday, June 6, at the beginning of class!
Announce that this program plays the game Higher/Lower. Then ask the player for his or her name and read it in. Greet the player and wish them luck. Then your program will select a number between 1 and 100 at random. Tell the user to guess the number. Read in the guess and tell them whether the actual number is higher or lower. If the guess was the actual number then tell them that they won and report the number of guesses it took them to guess the number. Also, use a sentinel value to let them quit the game if they don't want to continue playing. At the end of each game (by quitting, or correct guess), prompt to see if the user would like to play another game. Keep playing games until the user decides to stop. Don't forget to check the input to determine if it's in the valid range of 1 to 100 and prompt them again if it's not. Assume they will actually type in a number and don't worry about what happens if they type in a name or other such foolishness.
EXTRA CREDIT: For extra credit you can keep track of the number of guesses that the user made and report this when they win or quit. (So you would say something like "You won! It took you 15 guesses to find the number." when they won or "You have chosen to quit the game after taking 3 guesses." when they quit)
import java.io.*;Next you have to change the line with the main method header from this
public static void main(String[] args)to this
public static void main(String[] args) throws IOExceptionDon't forget that the capitalization is important! Now you can go ahead and use the statements we talked about in class. First, you have to put the setup code that we talked about at the top of main, right after the opening brace:
BufferedReader stdin = new BufferedReader( new InputStreamReader( System.in ));Then, you need to declare a variable for whatever type of data you want to read in and use the correct statement to read in that data type:
For String:
String name;For int:
name = stdin.readLine();
int number;
number = Integer.parseInt(stdin.readLine());
First, put the following at the top of the file, right after the block comment that has the program name, your name, etc. Look at P1 and P2 to see where I put the import statement for those two programs.
import java.util.Random;Next, use the following code to create an object of class Random (this would go near the beginning of the main method):
Random randomNum = new Random();Then, this line will generate a random number and store it in a variable called myNum (change myNum to whatever you call your variable):
myNum = Math.abs(randomNum.nextInt()) % 100; // generates an integer from 0 to 99
myNum = myNum + 1; // adding 1 makes it so the numbers are from 1 to 100
Here's an example of the sort of thing your program should do if there is incorrect input (once again, it doesn't have to match word for word). Remember that the message saying the number of guesses is extra credit. You have to print out a message saying that they chose to quit, but it doesn't HAVE to indicate the number of guesses.
Sample output with the extra credit done can be found here.