Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**************************************************************************************************
- * Program Name: Programming Project 4.8: Hi-Lo Game
- * Author: Terry Weiss
- * Date Written: September 26, 2015
- * Program Description:
- * Design and implement an application that plays the Hi-Lo guessing game with numbers. The
- * program should pick a random number between 1 and 100 (inclusive) and then repeatedly prompt
- * the user to guess the number. On each guess, report to the user that he or she is correct or
- * that the guess is high or low. Continue accepting guesses until the user guesses correctly or
- * chooses to quit. Use a sentinel value to determine whether the user wants to quit. Count the
- * number of guesses, and report that value when the user guesses correctly. At the end of each
- * game (by quitting or a correct guess), prompt to determine whether the user wants to play
- * again. Continue playing games until the user chooses to stop. When the user stops playing, tell
- * them how many games they've played.
- **************************************************************************************************/
- import java.util.Scanner;
- public class HiLoGame
- {
- public static boolean userQuit( String str )
- {
- return (str.equals("q") || str.equals("quit"));
- }
- public static void main( String[] args )
- {
- final int MIN_NUMBER = 1, MAX_NUMBER = 100, INVALID_GUESS = -1;
- int correctNumber, guessedNumber, guesses, games;
- String guess, cont;
- Scanner input = new Scanner(System.in);
- games = 0;
- // Loop to keep playing until ready to quit
- do
- {
- correctNumber = MIN_NUMBER + (int)(Math.random() * MAX_NUMBER);
- // System.out.println("Debug: Current number is: " + correctNumber);
- guesses = 0;
- // Loop to keep guessing until correct or quit
- do
- {
- System.out.print("Please enter your guess between " + MIN_NUMBER + " and "
- + MAX_NUMBER + " (\"q\" to quit): ");
- guess = input.next();
- if (!userQuit(guess))
- {
- try {
- guessedNumber = Integer.parseInt(guess);
- if (guessedNumber < MIN_NUMBER || guessedNumber > MAX_NUMBER)
- {
- System.out.print("That's not a valid guess. ");
- }
- else
- {
- if (guessedNumber > correctNumber)
- {
- System.out.println("Your guess was too high.");
- }
- else if (guessedNumber < correctNumber)
- {
- System.out.println("Your guess was too low.");
- }
- guesses++;
- }
- } catch (NumberFormatException e) {
- System.out.print("That's not a number! ");
- guessedNumber = INVALID_GUESS;
- }
- }
- else
- {
- guessedNumber = INVALID_GUESS;
- }
- } while ((guessedNumber != correctNumber) && !userQuit(guess));
- if (guessedNumber == correctNumber) // If it ended because they guessed correctly
- {
- String guessStr = (guesses == 1) ? "guess" : "guesses";
- System.out.println("Congratulations! " + correctNumber + " is correct. You guessed "
- + "this in " + guesses + " " + guessStr + ".");
- }
- else // Otherwise they quit
- {
- System.out.println("The correct answer was " + correctNumber + ".");
- }
- games++;
- System.out.print("Keep playing? ");
- cont = input.next();
- System.out.println();
- } while (cont.equals("y") || cont.equals("yes"));
- String gameStr = (games == 1) ? "game" : "games";
- System.out.println("Thanks for playing! You played " + games + " " + gameStr + ".");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment