fork download
  1. /*
  2.   Created by Zachary Wander on 9/7/2016.
  3.  */
  4.  
  5.  
  6. import java.util.InputMismatchException;
  7. import java.util.Scanner;
  8.  
  9. import java.util.Random;
  10.  
  11. import static java.lang.Thread.sleep;
  12.  
  13. class numbergame {
  14.  
  15. //main function
  16. public static void main(String[] args) {
  17. while (true) { //keep game going
  18. try {
  19. Scanner in = new Scanner(System.in); //setup user input
  20.  
  21. //intro
  22. System.out.println("Welcome to the Number Games.");
  23. System.out.println("Do you want to:");
  24. System.out.println("A) Guess a randomly generated number (1-100)?");
  25. System.out.println("B) Have the computer guess a number you come up with, within a range you specify?");
  26. System.out.println("C) Quit?");
  27. System.out.println("Choice:");
  28.  
  29. String choice = in.nextLine(); //take user input
  30.  
  31. //handle choice and function passing
  32. if (choice.toUpperCase().equals("A")){
  33. humanGuess();
  34. }
  35. else if (choice.toUpperCase().equals("B")){
  36. systemGuess();
  37. }
  38. else if (choice.toUpperCase().equals("C")){
  39. System.out.println("BYE!");
  40. break;
  41. }
  42. else {
  43. System.out.println("Something happened!"); //so I know if there's an error
  44. }
  45. }
  46. finally {} //this needs to be here for something
  47. }
  48. }
  49.  
  50. //the number guessing function
  51. public static void humanGuess() {
  52. Scanner in = new Scanner(System.in); //setup user input
  53. int number = (int) (Math.random() * 100) + 1; //generate a random number from 1-100
  54. int guess; //define guess variable
  55. int counter = 0; //to count the guesses
  56.  
  57. //intro
  58. System.out.println("Guess a number between 1 and 100 (inclusive)");
  59. System.out.println("You have 7 guesses.");
  60.  
  61. System.out.println(number); //DEBUG PURPOSES REMOVE
  62.  
  63. while (true) {
  64. try {
  65.  
  66. if (counter != 0) { //only tell the guess count after the user has guessed
  67. System.out.println("You have guessed: " + counter + " time(s).");
  68. }
  69.  
  70. //if number of guesses is reached, quit
  71. if (counter >= 7) {
  72. System.out.println("Sorry, you've used all your guesses.");
  73. System.out.println("The number was: " + number);
  74. System.out.println("Press Enter to continue."); //pretty self explanatory
  75. in.nextLine();
  76. break;
  77. }
  78.  
  79. System.out.println("Your guess:"); //get guess
  80. guess = in.nextInt();
  81. in.nextLine(); //this fixes something when exiting
  82.  
  83. if ((guess < 1) || (guess > 100)) { //if the guess is out of bounds, tell the user
  84. System.out.println("\n");
  85. System.out.println("Read the instructions.");
  86. System.out.println("Press Enter to continue."); //pretty self explanatory
  87. }
  88.  
  89. if (guess > number) {
  90. System.out.println("It's lower."); //if the guess is too high, tell user
  91. } else if (guess < number) {
  92. System.out.println("It's Higher"); //if the guess is too low, tell user
  93. } else if (guess == number) {
  94. counter = counter + 1; //since it counts up at the end of the loop
  95. System.out.println("Correct!");
  96. System.out.println("Guesses:" + counter);
  97. System.out.println("Press Enter to continue."); //pretty self explanatory
  98. in.nextLine(); //I think this needs to be here
  99. break;
  100. }
  101. else {}
  102. counter = counter + 1; //raise the guess counter
  103.  
  104. } catch (InputMismatchException e) {//if user doesn't enter an integer, complain
  105. System.out.println("Unusable input. Please use integers.");
  106. System.out.println("Press Enter to continue."); //pretty self explanatory
  107. in.nextLine(); //dump the bad input
  108. break;
  109. }
  110. }
  111. }
  112.  
  113. public static void systemGuess() {
  114. Scanner in = new Scanner(System.in); //setup user input
  115.  
  116. System.out.println("Think of a number and specify a range (integers only).");
  117.  
  118. //define range variables
  119. int numlow = 0;
  120. int numhigh = 0;
  121.  
  122. //prompt for range
  123. System.out.println("Enter the lowest number in the range.");
  124. if (in.hasNextInt()) {
  125. numlow = in.nextInt();
  126. } else { //catch non-ints
  127. System.out.println("Unusable input. Please use integers.");
  128. System.out.println("Press Enter to continue."); //pretty self explanatory
  129. in.nextLine(); //I think this needs to be here
  130. in.nextLine(); //dump the bad input
  131. }
  132.  
  133. System.out.println("Enter the highest number in the range.");
  134. if (in.hasNextInt()) {
  135. numhigh = in.nextInt();
  136. } else { //catch non-ints
  137. System.out.println("Unusable input. Please use integers.");
  138. System.out.println("Press Enter to continue."); //pretty self explanatory
  139. in.nextLine(); //I think this needs to be here
  140. in.nextLine(); //I think this needs to be here
  141. in.nextLine(); //dump the bad input
  142. }
  143.  
  144. if (numhigh < numlow) { //catch if range is reversed
  145. System.out.println("Smaller number first.");
  146. System.out.println("Press Enter to continue."); //pretty self explanatory
  147. in.nextLine(); //I think this needs to be here
  148. in.nextLine(); //dump the bad input
  149. }
  150.  
  151. System.out.println("Range: " + numlow + " - " + numhigh + "\n\n"); //tell the user the range they specified
  152.  
  153. //instructions
  154. System.out.println("Time to guess your number. You will see a guess printed.");
  155. System.out.println("If it is too high, type \"lower.\"");
  156. System.out.println("If it is too low, type \"higher.\"");
  157. System.out.println("If it is correct, type \"correct.\" \n");
  158.  
  159. //make sure the user has read instructions
  160. System.out.println("Hit Enter to continue.");
  161. in.nextLine();
  162. in.nextLine(); //Is there a better way to do this?
  163.  
  164. guessLoop(numhigh, numlow); //call the guessing function
  165.  
  166. //gameover confirmation
  167. System.out.println("Hit Enter to go back to the menu.");
  168. in.nextLine();
  169. }
  170.  
  171. public static int randomInt(int numhigh, int numlow) {
  172. //create a random guess
  173. Random randomNum = new Random();
  174. int randNum = randomNum.nextInt((numhigh - numlow) + 1) + numlow; //generate number from given range
  175.  
  176. return randNum; //return the random guess
  177. }
  178.  
  179. public static void guessLoop(int numhigh, int numlow) {
  180. Scanner in = new Scanner(System.in); //setup user input
  181. int counter = 0;
  182. while(true) {
  183. try {
  184. int guess = randomInt(numhigh, numlow); //call the random number function
  185.  
  186. System.out.println("Guess: " + guess); //tell user the guess
  187.  
  188. System.out.println("Is the number: ");
  189. String response = in.nextLine();
  190.  
  191. if (response.toUpperCase().equals("LOWER")) {
  192. numhigh = guess - 1;
  193. } else if (response.toUpperCase().equals("HIGHER")) {
  194. numlow = guess + 1;
  195. } else if (response.toUpperCase().equals("CORRECT")) {
  196. System.out.println("Great!");
  197. System.out.println("It took " + (counter + 1) + " guesses to find the number.");
  198. break;
  199. }
  200.  
  201. counter += 1;
  202.  
  203. } finally{}
  204. }
  205. }
  206. }
Runtime error #stdin #stdout #stderr 0.06s 711680KB
stdin
Standard input is empty
stdout
Welcome to the Number Games.
Do you want to:
A) Guess a randomly generated number (1-100)?
B) Have the computer guess a number you come up with, within a range you specify?
C) Quit?
Choice:
stderr
Exception in thread "main" java.util.NoSuchElementException: No line found
	at java.util.Scanner.nextLine(Scanner.java:1540)
	at numbergame.main(Main.java:29)