Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.47 KB | None | 0 0
  1. package assignment3;
  2. import textio.TextIO;
  3. public class Assignment3 {
  4.  
  5.     public static void main(String[] args) {
  6.        
  7.         boolean playAgain = true;
  8.         double rNumber;
  9.         int guess;
  10.        
  11.        
  12.         do
  13.         {
  14.         // Assigning rNumber a RANDOM value
  15.             rNumber = (int)(Math.random() * 99) + 1;
  16.            
  17.         //Gets guess from user
  18.         System.out.println("I have chosen a number between 1 and 99. Try to guess the number: ");
  19.         guess = TextIO.getInt();
  20.        
  21.         // Checking if guess is VALID
  22.         while (guess < 1 || guess > 99)
  23.         {
  24.             System.out.println("Your guess was not within the number range of 1 - 99. Please enter a valid guess: ");
  25.             guess = TextIO.getInt();
  26.         }
  27.        
  28.            
  29.         // Checking if guess is too HIGH or too LOW
  30.         while (guess > rNumber || guess < rNumber)
  31.         {
  32.             if (guess > rNumber)
  33.             {
  34.                 System.out.println("Your guess was too high! Try again: ");
  35.                 guess = TextIO.getInt();
  36.             }
  37.             else if (guess < rNumber)
  38.             {
  39.                 System.out.println("Your guess was too low! Try again: ");
  40.                 guess = TextIO.getInt();
  41.             }
  42.         }
  43.        
  44.         //Checking if guess was CORRECT
  45.         if (guess == rNumber)
  46.         {
  47.             System.out.println("Congratulations, you are correct. " + (int)rNumber + " was the correct number.");
  48.         }
  49.        
  50.         // Asks user if they would like to play again
  51.         System.out.println("Would you like to play again? ");
  52.         playAgain = TextIO.getBoolean();
  53.        
  54.        
  55.         } while(playAgain);
  56.        
  57.         //Thanks user for playing
  58.         System.out.println("Thank you for playing.");
  59.        
  60.        
  61.            
  62.        
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement