【发布时间】:2017-07-27 15:40:41
【问题描述】:
这个简单的代码让我很难受。我的 while 条件总是被忽略并执行 print 语句。请帮忙。
package Checkpoints;
import java.util.Scanner;
public class Check05 {
public static void main (String[]args){
Scanner keyboard = new Scanner(System.in);
/**
* Write an input validation that asks the user to enter 'Y', 'y', 'N', or 'n'.
*/
String input, Y = null, N = null;
System.out.println("Please enter the letter 'Y' or 'N'.");
input = keyboard.nextLine();
while (!input.equalsIgnoreCase(Y) || !(input.equals(N)))
//|| input !=y || input !=N ||input !=n)
{
System.out.println("This isn't a valid entry. Please enter the letters Y or N" );
input = keyboard.nextLine();
}
}
}
【问题讨论】:
-
你永远不会给 Y 或 N 赋值,然后在比较中使用它们。
-
您的
Y和N是空对象。没有任何东西等于null对象。 -
尝试使用调试器单步调试代码。
标签: java loops while-loop