如果我需要两个选项,如何正确使用布尔方法?

发布于 2025-01-13 04:14:28 字数 636 浏览 2 评论 0原文

问题是,如果我只需要从布尔方法中的两个选项(是或否)中进行选择,如何将其放入 IF 中? 我尝试这样做(见下文),它强调了最后一个大括号。如果我在 while 之外使用默认 return (但我不想),它会强调第一个 return (在第一个 if 之后)。

static boolean isAnotherGamer() {
    System.out.println("Play another game? Type in Y or N");
    Scanner scanner = new Scanner(System.in);
    String answer = scanner.nextLine();
    while (true) {
        if (answer.equalsIgnoreCase("Y")) {
            break;
            return true;
        } else if (answer.equalsIgnoreCase("N")) {
            break;
            return false;
        }
        System.out.println("Input mismatch");
    } //IDE underline this brace
}

The question is, if I need to chose only from two options in boolean method (Yes or No) how do I put it in IFs?
I try to do like this (see below), it underlines very last brace. If I use default return outside while (but I don't want to), it underlines first return (after first if).

static boolean isAnotherGamer() {
    System.out.println("Play another game? Type in Y or N");
    Scanner scanner = new Scanner(System.in);
    String answer = scanner.nextLine();
    while (true) {
        if (answer.equalsIgnoreCase("Y")) {
            break;
            return true;
        } else if (answer.equalsIgnoreCase("N")) {
            break;
            return false;
        }
        System.out.println("Input mismatch");
    } //IDE underline this brace
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

蒗幽 2025-01-20 04:14:28

我将这样做。这允许输入 yesno 的任何部分。我认为最好传递一个 Scanner 实例,而不是每次都创建一个。使用正则表达式可以让答案有一定的自由度。

  • ^$ - 字符串的开头和结尾。
  • (?i) - 忽略大小写
  • ye?s? - 表示必须有 yes 是可选的。
static boolean isAnotherGamer(Scanner scanner) {
    System.out.println("Play another game? Type in Y(es) or N(o)");
    while (true) {
        String input = scanner.nextLine();
        if (input.matches("(?i)^ye?s?$")) {
            return true;
        }
        if (input.matches("(?i)^no?$")) {
            return false;
        }
        System.out.println("Incorrect response, please enter Y(es) or N(o)");
    }       
}

Here is how I would do it. This allows any part of yes or no to be entered. I think it best to pass a Scanner instance rather than creating one each time. Using a regular expression allows for some latitude in the answer.

  • ^$ - beginning and end of string.
  • (?i) - ignore case
  • ye?s? - says must have y but e and s are optional.
static boolean isAnotherGamer(Scanner scanner) {
    System.out.println("Play another game? Type in Y(es) or N(o)");
    while (true) {
        String input = scanner.nextLine();
        if (input.matches("(?i)^ye?s?
quot;)) {
            return true;
        }
        if (input.matches("(?i)^no?
quot;)) {
            return false;
        }
        System.out.println("Incorrect response, please enter Y(es) or N(o)");
    }       
}
-小熊_ 2025-01-20 04:14:28

为什么不能先验证输入,然后在输入是或否后,再决定要做什么。如果不是,您可以使重复语句继续运行,直到获得所需的内容。 return 语句的位置是问题所在,因为如果 if 或 else if 语句不为 true,则该方法将不会像您的方法签名所暗示的那样返回布尔值,并且您的方法将只是一个无限循环。

Why can you not validate the input first, and then after the input is either a yes or no, decide on what to do. If it is not either, you can make the repetition statement continue to run until after you get what you need. The location of your return statement is the problem because if either if or else if statements are not true, the method will not return a boolean as your method signature suggests, and your method will just be an infinite loop.

紧拥背影 2025-01-20 04:14:28

您的方法被声明返回一个布尔值。流程中没有 return 语句。

假设您进入无限循环。此时我们评估用户输入的内容(为什么我们要在无限循环中这样做?答案在中间不会改变,不是吗?)

如果是“y”,我们就中断循环。
如果是“n”,我们就打破循环。
在任何其他情况下,我们都会打印一些内容并保留在循环中。

但一旦循环被打破 ->返回语句在哪里?

所以从我的角度来看,该函数应该如下所示:

static boolean isAnotherGamer() {
    Scanner scanner = new Scanner(System.in);

    while (true) {
        System.out.println("Play another game? Type in Y or N");
        String answer = scanner.nextLine();
        if (answer.equalsIgnoreCase("Y")) {
            return true;
        } else if (answer.equalsIgnoreCase("N")) {
            return false;
        }
        System.out.println("Input mismatch");
    }
}

Your method is declared to return a boolean. There is no return statement in the flow.

Assume you go into the endless loop. At this moment we evaluate what the user entered (why do we do that inside the endless loop? The answer does not change inbetween, does it?)

If it is 'y', we break the loop.
If it is 'n', we break the loop.
In any other case we print something and remain in the loop.

But as soon as the loop was broken -> where is the return statement?

So from my POV, the function should look like this:

static boolean isAnotherGamer() {
    Scanner scanner = new Scanner(System.in);

    while (true) {
        System.out.println("Play another game? Type in Y or N");
        String answer = scanner.nextLine();
        if (answer.equalsIgnoreCase("Y")) {
            return true;
        } else if (answer.equalsIgnoreCase("N")) {
            return false;
        }
        System.out.println("Input mismatch");
    }
}
装迷糊 2025-01-20 04:14:28

因为您尚未设置默认返回值,所以如果用户不选择“Y”或“N”,则不会返回任何内容,这就是您收到错误的原因。

此外,您不应该在break语句之后放置任何代码,因为这些行将被完全忽略(同样,由于return语句在break之后,所以不会返回任何内容。)

如果您只想完全删除这些break语句,则可以完全删除这些break语句一旦获得布尔值就退出该方法,或者如果您想继续在方法内运行代码,则可以更新布尔变量以供将来使用。 (我已经提供了一个例子)

        System.out.println("Play another game? Type in Y or N");
        Scanner scanner = new Scanner(System.in);
        String answer = scanner.nextLine();

        //To store the state of the user's answer
        boolean providedAnswer = false;

        //if the answer was yes, set the boolean's val to true
        if(answer.equalsIgnoreCase("Yes")){
            providedAnswer = true;
        }

        //output the boolean's value
        System.out.println("User wanted to play again? " + providedAnswer);

        //return the boolean value
        return providedAnswer;
    }```

Because you've not set a default return value, if the user doesn't choose either "Y" or "N" then nothing is going to be returned so that's why you're getting an error.

Additionally, you shouldn't be putting any code after your break statements as those lines will be completely ignored (again, nothing returned as your return statements are after your breaks.)

You can just completely remove those break statements if you're just wanting to quit that method once you've got your boolean value or you can update a boolean variable for future use if you're wanting to keep running code inside your method. (I've provided an example of this)

        System.out.println("Play another game? Type in Y or N");
        Scanner scanner = new Scanner(System.in);
        String answer = scanner.nextLine();

        //To store the state of the user's answer
        boolean providedAnswer = false;

        //if the answer was yes, set the boolean's val to true
        if(answer.equalsIgnoreCase("Yes")){
            providedAnswer = true;
        }

        //output the boolean's value
        System.out.println("User wanted to play again? " + providedAnswer);

        //return the boolean value
        return providedAnswer;
    }```
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文