Created
September 17, 2021 06:54
-
-
Save punkmonday/cc1a0c09decc69d12e6260c4c7f06297 to your computer and use it in GitHub Desktop.
given a number let machine guess don't lie
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Random; | |
import java.util.Scanner; | |
/** | |
* given a number let machine guess don't lie | |
* 0 - true 1 -false | |
*/ | |
public class MachineGuessNumber { | |
public static void main(String args[]) { | |
boolean right = false; | |
Scanner keyboard = new Scanner(System.in); | |
int min = 0; | |
int max = 1000; | |
int number = givenANumber(min, max); | |
while (!right) { | |
System.out.println(number); | |
System.out.println("right? 0-true 1-false"); | |
int i = keyboard.nextInt(); | |
if (i == 0) { | |
right = true; | |
System.out.println("exit."); | |
} else { | |
System.out.println("lower? 0-true 1-false"); | |
int lower = keyboard.nextInt(); | |
if (lower == 0) { | |
min = number + 1; | |
} else { | |
max = number - 1; | |
} | |
number = givenANumber(min, max); | |
} | |
} | |
} | |
public static int givenANumber(int min, int max) { | |
Random r = new Random(); | |
return r.ints(min, (max + 1)).findFirst().getAsInt(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment