brainfrz

Hi-Lo Game

Sep 26th, 2015
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.45 KB | None | 0 0
  1. /**************************************************************************************************
  2.  *  Program Name:     Programming Project 4.8: Hi-Lo Game
  3.  *  Author:           Terry Weiss
  4.  *  Date Written:     September 26, 2015
  5.  *  Program Description:
  6.  *     Design and implement an application that plays the Hi-Lo guessing game with numbers. The
  7.  *  program should pick a random number between 1 and 100 (inclusive) and then repeatedly prompt
  8.  *  the user to guess the number. On each guess, report to the user that he or she is correct or
  9.  *  that the guess is high or low. Continue accepting guesses until the user guesses correctly or
  10.  *  chooses to quit. Use a sentinel value to determine whether the user wants to quit. Count the
  11.  *  number of guesses, and report that value when the user guesses correctly. At the end of each
  12.  *  game (by quitting or a correct guess), prompt to determine whether the user wants to play
  13.  *  again. Continue playing games until the user chooses to stop. When the user stops playing, tell
  14.  *  them how many games they've played.
  15.  **************************************************************************************************/
  16.  
  17. import java.util.Scanner;
  18.  
  19.  
  20. public class HiLoGame
  21. {
  22.     public static boolean userQuit( String str )
  23.     {
  24.         return (str.equals("q") || str.equals("quit"));
  25.     }
  26.  
  27.    
  28.    
  29.     public static void main( String[] args )
  30.     {
  31.         final int MIN_NUMBER = 1, MAX_NUMBER = 100, INVALID_GUESS = -1;
  32.         int correctNumber, guessedNumber, guesses, games;
  33.         String guess, cont;
  34.         Scanner input = new Scanner(System.in);
  35.        
  36.        
  37.         games = 0;
  38.        
  39.         // Loop to keep playing until ready to quit
  40.         do
  41.         {
  42.             correctNumber = MIN_NUMBER + (int)(Math.random() * MAX_NUMBER);
  43. //          System.out.println("Debug: Current number is: " + correctNumber);
  44.             guesses = 0;
  45.            
  46.            
  47.             // Loop to keep guessing until correct or quit
  48.             do
  49.             {
  50.                 System.out.print("Please enter your guess between " + MIN_NUMBER + " and "
  51.                         + MAX_NUMBER + " (\"q\" to quit):  ");
  52.                 guess = input.next();
  53.                
  54.                 if (!userQuit(guess))
  55.                 {
  56.                     try {
  57.                         guessedNumber = Integer.parseInt(guess);
  58.                         if (guessedNumber < MIN_NUMBER || guessedNumber > MAX_NUMBER)
  59.                         {
  60.                             System.out.print("That's not a valid guess. ");
  61.                         }
  62.                         else
  63.                         {
  64.                             if (guessedNumber > correctNumber)
  65.                             {
  66.                                 System.out.println("Your guess was too high.");
  67.                             }
  68.                             else if (guessedNumber < correctNumber)
  69.                             {
  70.                                 System.out.println("Your guess was too low.");
  71.                             }
  72.                             guesses++;
  73.                         }
  74.                     } catch (NumberFormatException e) {
  75.                         System.out.print("That's not a number! ");
  76.                         guessedNumber = INVALID_GUESS;
  77.                     }
  78.                 }
  79.                 else
  80.                 {
  81.                     guessedNumber = INVALID_GUESS;
  82.                 }
  83.             } while ((guessedNumber != correctNumber) && !userQuit(guess));
  84.  
  85.             if (guessedNumber == correctNumber) // If it ended because they guessed correctly
  86.             {
  87.                 String guessStr = (guesses == 1) ? "guess" : "guesses";
  88.                
  89.                 System.out.println("Congratulations! " + correctNumber + " is correct. You guessed "
  90.                         + "this in " + guesses + " " + guessStr + ".");
  91.             }
  92.             else // Otherwise they quit
  93.             {
  94.                 System.out.println("The correct answer was " + correctNumber + ".");
  95.             }
  96.            
  97.             games++;
  98.             System.out.print("Keep playing?  ");
  99.             cont = input.next();
  100.             System.out.println();
  101.         } while (cont.equals("y") || cont.equals("yes"));
  102.        
  103.         String gameStr = (games == 1) ? "game" : "games";
  104.         System.out.println("Thanks for playing! You played " + games + " " + gameStr + ".");
  105.     }
  106.    
  107. }
Advertisement
Add Comment
Please, Sign In to add comment