Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. /*Evan Hopkins
  2. * Comp Sci
  3. * Reverse Guessing Game 6-2
  4. */
  5.  
  6. import java.util.Scanner;
  7.  
  8. public class ReverseGuessingGame {
  9.  
  10. public static void main(String[] args) {
  11. Scanner reader = new Scanner(System.in);
  12.  
  13. int number = 50, guess = 0, lcv = 0;
  14. int attempts = 0;
  15. int max = 100;
  16. int min = 1;
  17.  
  18. System.out
  19. .println("You are going to play me (the computer) in a guessing game. Enter "
  20. + "the number you would like me to guess and i will tell you how many tries "
  21. + "it takes me to get it. To help me, i will know whether the number is higher"
  22. + "or lower than my previous guess.");
  23. number = reader.nextInt();
  24.  
  25. while ((guess != number) && (lcv < 7)) {
  26. guess = Math.round(((max + min) / 2));// rounds the average of the
  27. // min and max possibilities
  28. System.out.println(guess);// prints all guesses
  29. if (guess > number)
  30. max = guess;
  31. else
  32. // guess is less than number
  33. min = guess;
  34. lcv++;
  35. attempts++;
  36. }
  37.  
  38. if (lcv > 7) {
  39. guess = 100;
  40. attempts = 7;
  41. System.out.println(guess);// prints guess
  42. }
  43.  
  44. System.out.println("The number is " + guess + " and it took me "
  45. + attempts + " tries to get it.");
  46. }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement