当我输入A、B、C或D时,仍然显示“这不是一个有效的答案”。为什么?

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

When I enter A, B, C, or D, it still says That is not a valid answer. Why?

问题

这是我为学校做的项目。我们被要求制作一个小测验,我想确保当用户输入的内容超出选项范围时,他们会收到一个错误消息,指出这不是一个有效的答案,但无论我输入什么,它都显示无效。为什么呢?

```java
import java.util.Scanner;
class Main {
  public static void main(String[] args) {
  String answer;
  int score = 0;
  Scanner input = new Scanner(System.in);
  System.out.println("所有科目测验现在开始,请为接下来的10个问题输入正确的选项(字母、T或F)");
      System.out.println("问题1(数学):方程y=2(x-2)^2+5的顶点是什么?");
      System.out.println("A. (-2,5)" + "\n" + "B. (2, 5)" + "\n" + "C. (-2,-5)" + "\n" + "D. (2, -5)");
      answer = input.nextLine();
      answer = answer.toUpperCase();
      
      while (!answer.equals("A") || !answer.equals("B") || !answer.equals("C") || !answer.equals("D")) {
        System.out.println("这不是一个有效的回答");
        answer = input.nextLine();
        answer = answer.toUpperCase();
      }
        if (answer.equals("A")) {
          System.out.println("回答错误,正确答案是B");
        } else if (answer.equals("B")){
          System.out.println("正确");
          score += 1;
        } else if (answer.equals("C")){
          System.out.println("回答错误,正确答案是B");
        } else if (answer.equals("D")){
          System.out.println("回答错误,正确答案是B");
        }

  }
}
英文:

This is my project for school. We were asked to make a little quiz and I want to make sure when the user enters something outside of the options, they will get an error message saying that is not a valid answer, but whatever I enter it still says that is not valid. Why?

import java.util.Scanner;
class Main {
  public static void main(String[] args) {
  String answer;
  int score = 0;
  Scanner input = new Scanner(System.in);
  System.out.println("ALL SUBJECT QUIZ Starting now, please enter the correct option (Letters or T or F) for the next 10 questions");
      System.out.println("Question 1 (Maths): What is the vertex of the equation y=2(x-2)^2+5");
      System.out.println("A. (-2,5)" + "\n" + "B. (2, 5)" + "\n" + "C. (-2,-5)" + "\n" + "D. (2, -5)");
      answer = input.nextLine();
      answer = answer.toUpperCase();
      
      while (!answer.equals("A") || !answer.equals("B") || !answer.equals("C") || !answer.equals("D")) {
        System.out.println("That is not a valid response");
        answer = input.nextLine();
        answer = answer.toUpperCase();
      }
        if (answer.equals("A")) {
          System.out.println("Incorrect, the answer is B");
        } else if (answer.equals("B")){
          System.out.println("Correct");
          score += 1;
        } else if (answer.equals("C")){
          System.out.println("Incorrect, the answer is B");
        } else if (answer.equals("D")){
          System.out.println("Incorrect, the answer is B");
        }

  }
}

Any help would be appreciated!

答案1

得分: 3

让我们评估您的以下条件:

!answer.equals("A") || !answer.equals("B") || !answer.equals("C") || !answer.equals("D")

假设,answer = E

现在,您的条件将被评估为

true || !answer.equals("B") || !answer.equals("C") || !answer.equals("D") => true

请注意,当多个条件用||连接时,一旦找到一个true条件,评估就会停止。

假设,answer = B

现在,您的条件将被评估为

true || !answer.equals("B") || !answer.equals("C") || !answer.equals("D") => true

假设,answer = A

现在,您的条件将被评估为

false || true || !answer.equals("C") || !answer.equals("D") => true

因此,您可以看到,无论答案如何,条件总是评估为true

解决方案:

将其更改为

while (!(answer.equals("A") || answer.equals("B") || answer.equals("C") || answer.equals("D")))

这意味着答案不是(A、或B、或C、或D

或者,您可以将其更改为

while (!answer.equals("A") && !answer.equals("B") && !answer.equals("C") && !answer.equals("D"))

这意味着答案不是A,且答案不是B,且答案不是C,且答案不是D

然而,为了简单起见,我偏好第一种方式。

英文:

Let's evaluate your following condition:

!answer.equals("A") || !answer.equals("B") || !answer.equals("C") || !answer.equals("D")

Assume, answer = E

Now, your condition will be evaluated as

true || !answer.equals("B") || !answer.equals("C") || !answer.equals("D") => true

Note that when multiple conditions are joined with ||, the evaluation stops as soon as a true condition is found.

Assume, answer = B

Now, your condition will be evaluated as

true || !answer.equals("B") || !answer.equals("C") || !answer.equals("D") => true

Assume, answer = A

Now, your condition will be evaluated as

false || true || !answer.equals("C") || !answer.equals("D") => true

Thus, you see that irrespective of the answer, the condition always evaluates to true.

Solution:

Change it to

while (!(answer.equals("A") || answer.equals("B") || answer.equals("C") || answer.equals("D")))

which means the answer is not (A, or B, or C, or D)

Alternatively, you can change it to

while (!answer.equals("A") && !answer.equals("B") && !answer.equals("C") && !answer.equals("D"))

which means the answer is not A and the answer is not B and the answer is not C and the answer is not D.

However, for the sake of simplicity, my taste is the first one.

答案2

得分: 1

如果你看一下你的 while 循环条件,你会注意到它将始终评估为 true,因为用户输入不能同时等于 ABCD

因此,解决方案是将条件中的逻辑或 (||) 改为逻辑与 (&&),因为你希望用户输入仅为以下之一:ABCD

while (!answer.equals("A") && !answer.equals("B") && !answer.equals("C") && !answer.equals("D")) {
    System.out.println("That is not a valid response");
    answer = input.nextLine();
    answer = answer.toUpperCase();
}
英文:

If you look at your while loop condition, you will notice that it will always evaluate to true because the user input can't equal A, B, C, and D all at once.

So the solution here is to change the or's (||) in the condition to and's (&&) since you want the user input to be only one of the following: A, B, C, or D.

while (!answer.equals("A") && !answer.equals("B") && !answer.equals("C") && !answer.equals("D")) {
    System.out.println("That is not a valid response");
    answer = input.nextLine();
    answer = answer.toUpperCase();
}

huangapple
  • 本文由 发表于 2020年10月27日 05:41:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/64545378.html
匿名

发表评论

匿名网友

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

确定