重新询问用户输入“是”或“否”的方法?

huangapple go评论110阅读模式
英文:

How to re-ask the user to enter Yes or No?

问题

private boolean askYesNo() {
    while (true) {
        final var answer = scanner.nextLine().toUpperCase().charAt(0);
        if (answer == 'Y' || answer == 'N') {
            return answer == 'Y';
        }
        System.out.println(pickMessage(new String[]{
                "C'mon, make a choice: yes or no?",
                "Don't leave me hanging! yes or no?",
                "Time to shine! Type yes or no.",
                "Ready to roll? Tell me yes or no!",
                "Give me some love! Yay or nay?"
        }));
    }
}

private String pickMessage(final String[] messages) {
    return messages[random.nextInt(messages.length)];
}
英文:

A simple text game expects input from the user in a YES or NO format.
If the format is incorrect, the program should ask again.
I want the game to use different funny phrases instead of boring ones like "Please enter yes or no".

I know how to code it in Java but English is not my native language and I find it difficult to come up with such phrases. I would be glad if you can help me come up with these phrases in English. May be some ideas where I can find such phrases.

    private boolean askYesNo() {
        while (true) {
            final var answer = scanner.nextLine().toUpperCase().charAt(0);
            if (answer == 'Y' || answer == 'N') {
                return answer == 'Y';
            }
            System.out.println(pickMessage(new String[]{
                    "Come on, yes or no?",
                    "Please enter yes or no."
            }));
        }
    }

    private String pickMessage(final String[] messages) {
        return messages[random.nextInt(messages.length)];
    }

答案1

得分: 2

首先,让我们修复您现有代码中的一些错误:

  1. 您缺少一个 else 块,这意味着 if 块后面的代码将始终执行;
  2. 不管输入是 Y 还是 N,您都在赋值 answer == 'Y'; 并返回它;
  3. 返回未转换的(类型未推断)类型,预期应该是一个 Character(所以,您应该进行转换),但您的方法声明要返回完全不同的类型 - boolean

现在让我们从您的数组中选择一个随机的字符串元素,同时修复现有的问题:

private Character askYesNo() {
    while (true) {
        final char answer = scanner.nextLine().toUpperCase().charAt(0);
        if (answer == 'Y' || answer == 'N') {
            return answer;
        } else {
            pickMessage(messages); // 假设您有一个名为 messages 的变量
        };
    }
}

private String pickMessage(final String[] messages) {
    Random random = new Random();
    return messages[random.nextInt(messages.length)];
}

这是修复后的代码。

英文:

First of all, let's fix the few mistakes in your existing code:

  1. You are missing an else block, which means, that lines after the if block will always execute;
  2. Disregarding of whether the input is Y or N, you're assigning answer == 'Y'; and returning that;
  3. Returning uncasted (type is not inferred) type, which is expected to be a Character (so, you should cast it), yet your method declares to be returning completely different type - boolean.

Now let's select a random String element from your array, by also fixing existing problems:

private Character askYesNo() {
    while (true) {
        final char answer = scanner.nextLine().toUpperCase().charAt(0);
        if (answer == 'Y' || answer == 'N') {
            return answer;
        } else {
            pickMessage(messages); //assuming that you have a messages variable
        };
    }
}

private String pickMessage(final String[] messages) {
    Random random = new Random();
    return messages[random.nextInt(messages.length)];
}

huangapple
  • 本文由 发表于 2020年8月23日 03:26:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63540272.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定