我正在做一個Java程序,需要涉及一些錯誤檢查。進入一個無效的字符串雖然不(或相當)java
while (n == 0){
System.out.println("Can't use 0 as a denominator! Please enter a real, nonzero number");
n = input.nextInt();
}
但我怎麼阻止用戶:我可以進入壞數值輸入像這樣(假設input
掃描儀已經創建)阻止用戶?我不能使用!=
,因爲字符串只能與string.equals()
方法比較,對吧?那麼,有沒有一段時間沒有循環?即:
while !(string.equals("y") || string.equals("n")){
//here have code
}
或者這種性質的東西?