Firstly, thanks for reading my post and helping me out.
I'm trying to program where I define a random number, the program guesses this random number, and then, depending on whether I say the guess is too high or low, the program guesses again.
When the program guesses a number which is too high, the user is to type "lower" and then the program to only guess numbers which are lower.
Ideally, the program would consider the results of all previous choice. So if guess #1 was too high, this should remain the upper-bound for all iterations afterwards. We don't want only the upper or lower bound changing with every iteration.
What I've tried to do with my code is reset the min and max values used to compute the random number ( ThreadLocalRandom ).
The main issue is that I cannot get the oMin and oMax values to reset. I don't understand how to get around this, sorry if this is a totally silly question.
Thanks again!
import java.util.Scanner;
import java.util.concurrent.ThreadLocalRandom;
public class J4AB_S11_C3 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int oMin = 0;
int oMax = 101;
int randomNumber = ThreadLocalRandom.current().nextInt(oMin, oMax);
String userReply;
System.out.println("What's the lucky number, kiddo?");
int correctAnswer = scanner.nextInt();
System.out.println("Is " + randomNumber + " the right answer?");
userReply = scanner.next();
while ("Higher".equalsIgnoreCase(userReply)) {
oMin = randomNumber;
}
while ("Lower".equalsIgnoreCase(userReply)) {
oMax = randomNumber;
}
if (userReply.equalsIgnoreCase("Correct")) {
System.out.println("We finally did it! " + correctAnswer + " was the correct answer.");
}
}
}
Aucun commentaire:
Enregistrer un commentaire