Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.Random;
- public class GuessingGame
- {
- public static void main(String[]args)
- {
- Random randomNumber = new Random();
- Scanner scan= new Scanner(System.in);
- int ranNumber;
- int guess;
- int tries=0;
- int playAgain=0;
- int numGuesses=0;
- int wins=0;
- double percentage;
- System.out.println("Hello! Let's play the Hi-Lo Guessing Game!");
- System.out.println("First, here are the rules...");
- System.out.println();
- System.out.println("I will think of a number from 1 to 10 and then ask you to guess the number. If you guess the number correctly, then I will print a congratulatory message and count the game as a win to you! However, if you guess incorrectly, then I will print either High or Low according to whether or not your guess was - you guessed it - too high or too low.");
- System.out.println();
- System.out.println("You will have three tries to guess the number before it counts as a loss.");
- System.out.println();
- System.out.println("At the end of the game, I will ask you if you want to play again.");
- System.out.println();
- System.out.println("You can play the game as many times as you like.");
- System.out.println();
- System.out.println("Once you indicate that you do not want to play again, I will print the number of games that you have played, the number of times you won the game, and the number of times that you lost the game.");
- System.out.println();
- do
- {
- System.out.println("Okay, let's get started!!");
- ranNumber= randomNumber.nextInt(10)+1;
- do
- {
- System.out.println("I am thinking of a number from 1 to 10...");
- System.out.println();
- System.out.println("What do you think it is?");
- guess = scan.nextInt();
- System.out.println("Your guess is " + guess);
- numGuesses++;
- if( guess < ranNumber)
- {
- System.out.println("You guessed too low!");
- }
- else if(guess > ranNumber)
- {
- System.out.println("You guessed too high!");
- }
- else
- {
- System.out.println("That's right! You win!");
- wins++;
- }
- }
- while (guess != ranNumber);
- System.out.println("The correct number is " + ranNumber);
- System.out.println("Would you like to play again?\nEnter 1 for yes or 2 for no");
- playAgain = scan.nextInt();
- tries++;
- }
- while (playAgain == 1);
- System.out.println("Thanks for playing!!");
- System.out.println("You won " + wins +" games out of " + tries +" games");
- percentage = wins/tries*100;
- System.out.println("This means you won " + percentage+ " % of your games");
- if ( percentage >= 90)
- {
- System.out.println("Congratulations!! Your a SuperStar!!");
- }
- else
- {
- System.out.println("Sorry, but this does not make you a SuperStar.\nTo be a superstar you have to win at least 90% of your games!");
- }
- }
- }
Add Comment
Please, Sign In to add comment