Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. //Game
  2.  
  3. System.out.println("Pick a number between 1-100");
  4.  
  5. Scanner keyboard = new Scanner(System.in);
  6. Random rand = new Random();
  7. int number = rand.nextInt(100)+1;
  8. int round = 0;
  9. int count = 0;
  10. int guess = 0;
  11. int win = 0;
  12.  
  13. while(win == 0)
  14. {
  15. round++;
  16. System.out.println("Round " + round);
  17.  
  18. System.out.print("What is your first guess? ");
  19. guess = keyboard.nextInt();
  20. count++;
  21.  
  22. if (guess == number)
  23. {
  24. if (count == 1)
  25. {
  26. System.out.println("You win in " + count + " guess.");
  27. ++win;
  28. break;
  29. }
  30. }
  31. else if (guess > number)
  32. {
  33. System.out.println("That's too high. Try again: ");
  34. }
  35. else if (guess < number)
  36. {
  37. System.out.println("That's too low. Try again: ");
  38. }
  39.  
  40. }
  41.  
  42.  
  43. //Ask to play again
  44. boolean isValidAnswer;
  45. do
  46. {
  47. System.out.print("Would you like to play again (yes/no)? ");
  48. String playAgain = keyboard.next().toUpperCase();
  49. isValidAnswer= playAgain.equals("YES") || playAgain.equals("NO");
  50. if(! isValidAnswer)
  51. {
  52. System.out.println("Error: Please enter yes or no");
  53. System.out.println();
  54. }
  55. }while(!isValidAnswer);
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement