Advertisement
Guest User

hw22

a guest
Oct 16th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.44 KB | None | 0 0
  1. import java.util.Scanner;
  2. /**
  3.  * This program asks the user to think of a number and uses the binary search strategy to guess
  4.  * the number the user is thinking of.
  5.  * @author
  6.  * @version 10/16/17 (initial)
  7.  *
  8.  */
  9. public class SecretNumber {
  10.  
  11.     public static void main(String[] args) {
  12.         Scanner input = new Scanner(System.in);
  13.         int min = 1;
  14.         int max = 100;
  15.         System.out.println("Think of a number from 1 to 100 and reply; correct, higher, or lower.");
  16.         int a = min;
  17.         int b = max;
  18.         System.out.println((a + b)/2);
  19.         int run = 0;
  20.         boolean correct = false;
  21.         while (run <= 6) {
  22.             String reply = input.nextLine();
  23.             if (reply.equals("correct")) {
  24.                 run = 7;
  25.                 correct = true;
  26.                 System.out.println("coolio");
  27.             }
  28.             else if (reply.equals("higher")) {
  29.                 a = (a + b)/2;
  30.                 System.out.println((a + b)/2);
  31.             }
  32.             else if (reply.equals("lower")) {
  33.                 b = (a + b)/2;
  34.                 System.out.println((a + b)/2);
  35.             }
  36.             else if (run == 6 && correct) {
  37.                 System.out.println("I think you're cheating");
  38.             }
  39.             else {
  40.                 System.out.println("Invalid input");
  41.             }
  42.             run++;
  43.         }
  44.         input.close();
  45.     }
  46.  
  47. }
  48.  
  49. /*
  50.  Think of a number from 1 to 100 and reply; correct, higher, or lower.
  51. 50
  52. lower
  53. 25
  54. higher
  55. 37
  56. higher
  57. 43
  58. higher
  59. 46
  60. higher
  61. 48
  62. higher
  63. 49
  64. correct
  65. coolio
  66.  
  67. Think of a number from 1 to 100 and reply; correct, higher, or lower.
  68. 50
  69. higher
  70. 75
  71. higher
  72. 87
  73. lower
  74. 81
  75. lower
  76. 78
  77. lower
  78. 76
  79. correct
  80. coolio
  81.  
  82.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement