Guest User

Untitled

a guest
Dec 14th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.Random;
  3.  
  4. public class GuessingGame
  5. {
  6. public static void main(String[]args)
  7. {
  8. Random randomNumber = new Random();
  9. Scanner scan= new Scanner(System.in);
  10.  
  11. int ranNumber;
  12. int guess;
  13. int tries=0;
  14. int playAgain=0;
  15. int numGuesses=0;
  16. int wins=0;
  17. double percentage;
  18.  
  19. System.out.println("Hello! Let's play the Hi-Lo Guessing Game!");
  20. System.out.println("First, here are the rules...");
  21. System.out.println();
  22. 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.");
  23. System.out.println();
  24. System.out.println("You will have three tries to guess the number before it counts as a loss.");
  25. System.out.println();
  26. System.out.println("At the end of the game, I will ask you if you want to play again.");
  27. System.out.println();
  28. System.out.println("You can play the game as many times as you like.");
  29. System.out.println();
  30. 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.");
  31. System.out.println();
  32. do
  33. {
  34. System.out.println("Okay, let's get started!!");
  35. ranNumber= randomNumber.nextInt(10)+1;
  36.  
  37.  
  38. do
  39. {
  40.  
  41. System.out.println("I am thinking of a number from 1 to 10...");
  42. System.out.println();
  43. System.out.println("What do you think it is?");
  44. guess = scan.nextInt();
  45. System.out.println("Your guess is " + guess);
  46. numGuesses++;
  47.  
  48. if( guess < ranNumber)
  49. {
  50. System.out.println("You guessed too low!");
  51. }
  52. else if(guess > ranNumber)
  53. {
  54. System.out.println("You guessed too high!");
  55. }
  56. else
  57. {
  58. System.out.println("That's right! You win!");
  59. wins++;
  60. }
  61.  
  62. }
  63. while (guess != ranNumber);
  64.  
  65. System.out.println("The correct number is " + ranNumber);
  66. System.out.println("Would you like to play again?\nEnter 1 for yes or 2 for no");
  67. playAgain = scan.nextInt();
  68. tries++;
  69.  
  70. }
  71. while (playAgain == 1);
  72.  
  73. System.out.println("Thanks for playing!!");
  74. System.out.println("You won " + wins +" games out of " + tries +" games");
  75. percentage = wins/tries*100;
  76. System.out.println("This means you won " + percentage+ " % of your games");
  77. if ( percentage >= 90)
  78. {
  79. System.out.println("Congratulations!! Your a SuperStar!!");
  80. }
  81. else
  82. {
  83. 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!");
  84. }
  85. }
  86. }
Add Comment
Please, Sign In to add comment