Skip to content

Instantly share code, notes, and snippets.

@archigoodvin07
Created October 18, 2021 18:29
Show Gist options
  • Save archigoodvin07/eb84d71e5962e9671663066d96e651d4 to your computer and use it in GitHub Desktop.
Save archigoodvin07/eb84d71e5962e9671663066d96e651d4 to your computer and use it in GitHub Desktop.
import java.util.Scanner;
class GuessTheNumber {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int min = 1;
int max = 100;
while (true) {
int guess = (min + max) / 2;
System.out.println("Hi. Can I try to guess. Your number " + guess + "?");
String line = s.nextLine();
if (line.equals(">")) { // Press ">" if your number is bigger
min = guess;
}
else if (line.equals("<")) { // Press "<" if your number is lower
max = guess;
} else if (line.equals("=")) { // Press "=" if your number is equal
System.out.println("Congrats!");
break;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment