我完全是Java的新手,我已經開始使用一些簡單的控制檯應用程序。雖然循環沒有完成
這是我當前的應用程序的代碼:
Scanner sc = new Scanner(System.in);
boolean ExitLoop = false;
ArrayList<Integer> IDs = new ArrayList<Integer>();
ArrayList<Double> averages = new ArrayList<Double>();
while(!ExitLoop)
{
System.out.println("StudentID: ");
IDs.add(sc.nextInt());
System.out.println("Average: ");
averages.add(sc.nextDouble());
System.out.println("Do you want to register another student? [y/n] ");
ExitLoop = (sc.next() == "n");
}
不好意思問這麼愚蠢的問題,但我真的卡在這,我打的「n」,但while循環沒有停止,繼續工作。我做錯了什麼?當用戶輸入「n」表示「否」時,我應該怎麼做來完成循環?
僅供參考,'do {...} while(!sc.next()。equals(「n」));''是你試圖做的直接構造,而不需要'ExitLoop'標誌。 –
作爲慣例開始您的變量名稱小寫:'exitLoop'和'ids'。否則有人可能會認爲這些是類。 – Simulant
+1 @MarkPeters好的意見,你的解決方案應該是答案。添加它作爲答案,以便我可以接受它。 –