Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2014
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1.  
  2.  
  3. package test;
  4.  
  5. import java.util.Random;
  6. import java.util.Scanner;
  7.  
  8. /**
  9. * <p>This is where you put your description about what this class does. You
  10. * don't have to write an essay but you should describe exactly what it does.
  11. * Describing it will help you to understand the programming problem better.</p>
  12. *
  13. * @author Your Name goes here
  14. * @version 1.0
  15. */
  16. public class Guess {
  17. /**
  18. * <p>This is the main method (entry point) that gets called by the JVM.</p>
  19. *
  20. * @param args command line arguments.
  21. */
  22. public static void main(String[] args) {
  23.  
  24. int guess;
  25. int number;
  26. int count = 0;
  27. String again;
  28. boolean playAgain = true;
  29.  
  30. Scanner scan = new Scanner(System.in);
  31. Random random = new Random();
  32.  
  33. number = random.nextInt(100) + 1;
  34. guess = -1;
  35.  
  36. while (playAgain) {
  37.  
  38. if (guess != number) {
  39.  
  40. System.out.println ("Guess a number from 0-100: ");
  41.  
  42. guess = scan.nextInt();
  43.  
  44. if (guess > number) {
  45.  
  46. System.out.println ("Too high, try again.");
  47. count++;
  48.  
  49. } else if (guess < number) {
  50.  
  51. System.out.println ("Too low, try again.");
  52. count++;
  53.  
  54. } else if (guess == number) {
  55.  
  56. System.out.println(guess + " was correct.");
  57. System.out.println("It took you " + count + " tries.");
  58. playAgain = false;
  59. }
  60. }
  61.  
  62. System.out.println("Woud you like to play again? (y/n)");
  63. again = scan.next();
  64.  
  65. if (again.equalsIgnoreCase("n")) playAgain = false;
  66. }
  67.  
  68. System.out.println("Question two was called and ran sucessfully.");
  69. }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement