Advertisement
Guest User

Untitled

a guest
Feb 21st, 2018
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. package numberGuesser;
  2.  
  3. import java.util.concurrent.ThreadLocalRandom;
  4. import java.util.Scanner;
  5.  
  6.  
  7. public class Guesser {
  8.  
  9. public static void main(String[] args) {
  10. // TODO Auto-generated method stub
  11. int yes = 1;
  12.  
  13. while (true) {
  14. // creates a random number each time the loop is restarted
  15. int randomNum = ThreadLocalRandom.current().nextInt(0, 10 + 1);
  16.  
  17. //lets the user guess the number
  18. Scanner input = new Scanner(System.in);
  19.  
  20.  
  21. try {
  22. System.out.println("Take a Guess to what my number is: ");
  23. String Guess = input.next();
  24. //converts the number to an integer
  25. int Guess_int = Integer.parseInt(Guess);
  26. }
  27. catch(NumberFormatException ex) {
  28. System.out.print("there was an error");
  29. }
  30.  
  31. // checks if the numbers are the same
  32. if (Guess_int == randomNum) {
  33.  
  34. //runs when the answer is correct
  35. System.out.println("correct");
  36.  
  37.  
  38. System.out.println("do you want to play again, answer 1 if yes or 0 if no: ");
  39. String answer = input.next();
  40. int answer_int = Integer.parseInt(answer);
  41.  
  42. //checks if the answer user wants to go again
  43. if(answer_int == yes) {
  44. // runs when the user answers yes or 1
  45. System.out.println("ok, lets go again");
  46. System.out.println("");
  47. System.out.println("");
  48. continue;
  49. }
  50.  
  51. else{
  52. // runs when the user answers anything other than yes or 1
  53. System.out.println("ok, goodbye");
  54. input.close();
  55. break;
  56. }
  57. }
  58.  
  59. else{
  60. // runs when the answer was wrong
  61. System.out.println("wrong, my number was: " + randomNum );
  62.  
  63. }
  64. }
  65. }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement