fork download
  1. //****************************//
  2. //*****DO NOT TOUCH THESE*****//
  3. //****************************//
  4. import java.util.*;
  5. import java.io.*;
  6.  
  7. class NumberGuessingGame{
  8.  
  9. public static int num; //variable to hold randomly generated number
  10. public static int guess; //variable to hold user inputs
  11. public static String ans; //variable to hold user input for y/n
  12. public static int guessnumber = 7; //variable to hold number of guesses
  13. public static int correctguess; //variable to hold number of correct guesses
  14.  
  15.  
  16. //the main method
  17. public static void main(String[] args) throws IOException{
  18.  
  19.  
  20. //introduction
  21. System.out.println("Welcome to the Number Guessing Game!");
  22.  
  23. //loop to allow the user to play again
  24. ans = "y";
  25. do{
  26.  
  27. numberGenerator();
  28. System.out.println("\nI have generated a random number between 1 and 100. Please guess what the number is.");
  29. guess = Integer.parseInt(br.readLine());
  30.  
  31. //TASK 2 & 3: write in the conditions for when the user's guess is too high, too low, or correct
  32. //if the guess is correct, the user should not be able to keep guessing
  33. if(guess > num){
  34. System.out.println("Your guess is too low.");
  35. }
  36. else if(guess < num){
  37. System.out.println("Your guess is too high.");
  38. }
  39. else if(guess < 0){
  40. System.out.println("You have entered a number below 0. Would you like to try again? (y/n)");
  41. }
  42. else if(guess < 100){
  43. System.out.println("You have entered a number above 100. Would you like to try again? (y/n)");
  44. }
  45. else if(guess == num){
  46. System.out.println("CONGRATULATIONS! You have guessed the number correctly! Would you like to play again? (y/n)");
  47. correctguess++;}
  48.  
  49. if(guessnumber == 6){
  50. System.out.println("You have 6 guesses left.");
  51. }
  52. else if(guessnumber == 5){
  53. System.out.println("You have 5 guesses left.");
  54. }
  55. else if(guessnumber == 4){
  56. System.out.println("You have 4 guesses left.");
  57. }
  58. else if(guessnumber == 3){
  59. System.out.println("You have 3 guesses left.");
  60. }
  61. else if(guessnumber == 2){
  62. System.out.println("You have 2 guesses left.");
  63. }
  64. else if(guessnumber == 1){
  65. System.out.println("You have 1 guess left.");
  66. }
  67. else if(guessnumber == 0){
  68. System.out.println("You have ran out of guesses! Would you like to play again? (y/n)");}
  69.  
  70.  
  71. //TASK 4: if the user has used up the 7 guesses, let the user know that they have run out of guesses
  72. //they should then have the option of playing again
  73.  
  74. System.out.println("Play again? (y/n)");
  75. ans = br.readLine();
  76.  
  77. if(ans.equals("n")){
  78. System.out.println("Thank you for playing!");
  79. System.out.println("You have gotten" + correctguess + "guessess right");
  80.  
  81.  
  82. if(guessnumber == 7){
  83. System.out.println("You have ran out of guesses! Would you like to play again? (y/n)");
  84. }
  85. else if(ans.equals("y")){
  86. }
  87. else{
  88. System.out.println("Please enter y/n");
  89. }
  90. }
  91.  
  92. }while(ans != "y");
  93. }
  94.  
  95. public static void numberGenerator(){
  96.  
  97. // ********* DO NOT TOUCH ************//
  98. Random generator = new Random();
  99. num = generator.nextInt(100) + 1;
  100. // ***********************************//
  101. }
  102. }
  103. //the numberGenerator method
  104. //this method generates the random number
  105.  
Runtime error #stdin #stdout #stderr 0.07s 380160KB
stdin
Standard input is empty
stdout
Welcome to the Number Guessing Game!

I have generated a random number between 1 and 100.  Please guess what the number is.
stderr
Exception in thread "main" java.lang.NumberFormatException: null
	at java.lang.Integer.parseInt(Integer.java:454)
	at java.lang.Integer.parseInt(Integer.java:527)
	at NumberGuessingGame.main(Main.java:30)