Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. import java.util.Random;
  2. import java.util.Scanner;
  3.  
  4. public class NumberGuesser {
  5. public static int randomizer() {
  6. Random rand = new Random ();
  7. int answer = rand.nextInt(100);
  8. System.out.println("The computer's answer is " + answer);
  9. return answer;
  10. }
  11.  
  12. public static String name() {
  13. //unnecessary; just scanner practice
  14. Scanner s = new Scanner(System.in);
  15. System.out.println("Let's play a guessing game! What's your name?");
  16. String userName= s.nextLine();
  17. System.out.println("Hi, " + userName + "! Okay, I'm thinking of a number between 1 and 100. You have ten tries to guess what it is.");
  18. return userName;
  19. }
  20.  
  21. public static boolean guesser(int randomNumber) {
  22. for(int i=1;i<=10;i++) {
  23. Scanner s = new Scanner(System.in);
  24. System.out.println("Try number " + i + ". Guess a number.");
  25. int userGuess= s.nextInt();
  26. if (userGuess < randomNumber && i <= 9) {
  27. System.out.println("Go higher.");
  28. }
  29. else if (userGuess < randomNumber && i == 10) {
  30. return false;
  31. }
  32. else if (userGuess > randomNumber && i <= 9) {
  33. System.out.println("Go lower.");
  34. }
  35. else if (userGuess > randomNumber && i == 10) {
  36. return false;
  37. }
  38.  
  39. else if (userGuess == randomNumber) {
  40. System.out.println("You got it!");
  41. return true;
  42. }
  43. }
  44. return false;
  45. }
  46.  
  47. public static int scoreTracker (boolean winOrLose, String userName) {
  48. int pcScore;
  49. int playerScore;
  50. for (pcScore = 0, playerScore = 0; pcScore <= 100, playerScore <= 100) {
  51. if (winOrLose == false) {
  52. System.out.println("You're out of guesses. My win!");
  53. pcScore++;
  54. }
  55. else {
  56. System.out.println(userName + " wins!");
  57. playerScore++;
  58. }
  59. }
  60.  
  61. return 3;
  62. }
  63.  
  64. public static void main (String[] args) {
  65. int randomNumber = randomizer();
  66. String userName = name();
  67. boolean winOrLose = guesser(randomNumber);
  68. scoreTracker(winOrLose, userName);
  69. randomNumber = randomizer();
  70. }
  71.  
  72.  
  73. }
  74.  
  75.  
  76.  
  77. /*Write a number guessing game. Your program should choose a random number between 0 and 99 (see below).
  78. * The user should then be given 10 guesses to guess the number, with your program telling them, on each guess,
  79. * whether they were too high, too low, or correct. To generate a random int between 0 and 99 use the following code:
  80. Random rand = new Random();
  81. int randomNum = rand.nextInt(100);
  82. And add the following line to the very top of your program (above the public class line):
  83. import java.util.Random;
  84.  
  85. *
  86. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement