很难理解为什么这段代码不会像我希望的那样运行。我正在创建一个带有2个数组的测试检查器,一个是密钥,另一个是用户的输入。我试图验证用户是否仅限于输入“A,B,C或D”。我尝试过使用布尔值,嵌套if和我发现的其他几种方法,但它们都没有正常工作。
package projects;
public static void main(String[] args) {
String key[] = {"B", "D","A", "A", "C", "A", "B", "A", "C", "D", "B", "C", "D", "A", "D", "C", "C", "B", "D", "A"};
String Input[] = new String[20];
for (int x = 0; x < 20; x++)
{
Input[x] = JOptionPane.showInputDialog(null, "Input Answers (A, B, C, or D)" );
if (Input[x] != "A" || Input[x] != "B" || Input[x] != "C" || Input[x] != "B") {
JOptionPane.showMessageDialog(null, "Please restrict input to 'A, B, C, or D'");
System.out.println(Input[x]);
System.exit(0);
}
else{
System.out.println("This should print if A, B, C, or D is entered!");
}
}
}
}