0
votes

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.");
        }

    }
}
2
When do you want the oMin and oMax to be reset and what are you trying to reset them to (0 and 101)? - Brenden Price
I want them to be reset when a guess is too high or too low. If the correct answer is 20, but the program guesses 40, I'd like 40 to be set as the new max. - Thomas Magnus
^ and I would like these changes to be preserved over time so that the scope of the guess continually becomes smaller. - Thomas Magnus
I think I understand, give me a few minutes to review your code and I'll try to post a solution. - Brenden Price
You're using 'while ("Higher".equalsIgnoreCase(userReply))' while when you should be using if. - NomadMaker

2 Answers

0
votes

Use do-while for this, if-condition to check higher and lower. And close scanner after the do-while

        Scanner scanner = new Scanner(System.in);
        int oMin = 0;
        int oMax = 101;
        String userReply;
        int correctAnswer;
        do {
            int randomNumber = ThreadLocalRandom.current().nextInt(oMin, oMax);
            System.out.println("What's the lucky number, kiddo?");
            correctAnswer = scanner.nextInt();

            System.out.println("Is " + randomNumber + " the right answer?");
            userReply = scanner.next();

            if ("Higher".equalsIgnoreCase(userReply)) {
                oMin = randomNumber;
            }else if ("Lower".equalsIgnoreCase(userReply)) {
                oMax = randomNumber;
            }
        } while (!userReply.equalsIgnoreCase("Correct"));

        System.out.println("We finally did it! " + correctAnswer + " was the correct answer.");

        scanner.close();
0
votes

I made a few edits and added a while loop, check it out. I tested it and it seems to be working.

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 (!"Correct".equalsIgnoreCase(userReply)){
        if ("Higher".equalsIgnoreCase(userReply)) {
            oMin = randomNumber;
        }

        if ("Lower".equalsIgnoreCase(userReply)) {
            oMax = randomNumber;
        }
        randomNumber = ThreadLocalRandom.current().nextInt(oMin, oMax);
        System.out.println("Is " + randomNumber + " the right answer?");
        userReply = scanner.next();
        }
        if (userReply.equalsIgnoreCase("Correct")) {
            System.out.println("We finally did it! " + correctAnswer + " was the correct answer.");
        }
    }
}