Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package assignment3;
- import textio.TextIO;
- public class Assignment3 {
- public static void main(String[] args) {
- boolean playAgain = true;
- double rNumber;
- int guess;
- do
- {
- // Assigning rNumber a RANDOM value
- rNumber = (int)(Math.random() * 99) + 1;
- //Gets guess from user
- System.out.println("I have chosen a number between 1 and 99. Try to guess the number: ");
- guess = TextIO.getInt();
- // Checking if guess is VALID
- while (guess < 1 || guess > 99)
- {
- System.out.println("Your guess was not within the number range of 1 - 99. Please enter a valid guess: ");
- guess = TextIO.getInt();
- }
- // Checking if guess is too HIGH or too LOW
- while (guess > rNumber || guess < rNumber)
- {
- if (guess > rNumber)
- {
- System.out.println("Your guess was too high! Try again: ");
- guess = TextIO.getInt();
- }
- else if (guess < rNumber)
- {
- System.out.println("Your guess was too low! Try again: ");
- guess = TextIO.getInt();
- }
- }
- //Checking if guess was CORRECT
- if (guess == rNumber)
- {
- System.out.println("Congratulations, you are correct. " + (int)rNumber + " was the correct number.");
- }
- // Asks user if they would like to play again
- System.out.println("Would you like to play again? ");
- playAgain = TextIO.getBoolean();
- } while(playAgain);
- //Thanks user for playing
- System.out.println("Thank you for playing.");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement