我想讓用戶只輸入「y」「Y」「n」或「N」。結果代碼要求用戶在輸入正確的輸入時輸入Y或N,與我期望的相反,將!在input.equalscaseignore(輸入)前面。使用while循環驗證輸入
import java.util.Scanner;
class inputVal {
public static void main(String[] args) {
String Input;
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter Y or N. ");
Input = keyboard.nextLine();
while (!Input.equalsIgnoreCase("Y") || !Input.equalsIgnoreCase("N"))
{
System.out.println("Please enter Y or N");
Input = keyboard.nextLine();
}
keyboard.close();
}
}
的問題是,如果用戶輸入N,則Input.equalsIgnoreCase(「Y」)成爲現實..和如果用戶輸入y時,輸入! equalsIgnoreCase(「N」)成爲true,所以它總是在 – 2014-10-17 17:24:03