我希望程序在用戶輸入一個小於零的值時立即結束,但此程序不斷詢問所有三個值然後退出。直到用戶輸入完所有值後,循環纔會輸入負數。
例如,如果我輸入-3,2,1,程序不進入-3第一高度後顯示錯誤,而不是需要所有三個值然後顯示「無效高度」消息。
如何讓程序顯示無效高度的錯誤信息,一旦用戶進入負值?
//Program Asking user to input three different heights
HeightOne = Integer.parseInt(JOptionPane.showInputDialog("Enter Height of First Tower"));
HeightTwo = Integer.parseInt(JOptionPane.showInputDialog("Enter Height of Second
Tower"));
HeightThree = Integer.parseInt(JOptionPane.showInputDialog("Enter Height of Third Tower"));
If (centimeterHeightOne < 0 || centimeterHeightTwo < 0 || centimeterThree < 0)
{
JOptionPane.showMessageDialog(null, "Invalid height";
}
else
{
conditions...
}
我明白,但問題是我不想記下錯誤消息三次。我想知道是否有任何爲什麼要把所有條件放在一起。 – user3315642
@ user3315642您不必打印三次。當你顯示錯誤時,你可以做任何你想做的事情。 – Maroun
有什麼辦法可以通過try/catch異常處理來做到嗎? – user3315642