Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- /**
- * This program asks the user to think of a number and uses the binary search strategy to guess
- * the number the user is thinking of.
- * @author
- * @version 10/16/17 (initial)
- *
- */
- public class SecretNumber {
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- int min = 1;
- int max = 100;
- System.out.println("Think of a number from 1 to 100 and reply; correct, higher, or lower.");
- int a = min;
- int b = max;
- System.out.println((a + b)/2);
- int run = 0;
- boolean correct = false;
- while (run <= 6) {
- String reply = input.nextLine();
- if (reply.equals("correct")) {
- run = 7;
- correct = true;
- System.out.println("coolio");
- }
- else if (reply.equals("higher")) {
- a = (a + b)/2;
- System.out.println((a + b)/2);
- }
- else if (reply.equals("lower")) {
- b = (a + b)/2;
- System.out.println((a + b)/2);
- }
- else if (run == 6 && correct) {
- System.out.println("I think you're cheating");
- }
- else {
- System.out.println("Invalid input");
- }
- run++;
- }
- input.close();
- }
- }
- /*
- Think of a number from 1 to 100 and reply; correct, higher, or lower.
- 50
- lower
- 25
- higher
- 37
- higher
- 43
- higher
- 46
- higher
- 48
- higher
- 49
- correct
- coolio
- Think of a number from 1 to 100 and reply; correct, higher, or lower.
- 50
- higher
- 75
- higher
- 87
- lower
- 81
- lower
- 78
- lower
- 76
- correct
- coolio
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement