简体   繁体   中英

Iterating a While Loop with User Input

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

    }
}

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();

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM