有人可以解釋如何在不使用中斷或system.exit的情況下點擊'capacity exceeded'消息後結束程序,但會繼續提示如果沒有達到信息,'離開'和'進入'?如何打印變量的先前狀態並退出循環而不中斷
此外,對於'超出容量'消息,它還顯示totalPeople。然而,本節中的總人數卻成爲我進入許多人離開或進入的人羣。我怎麼能這樣做,這是我在進入假期或輸入數值以使其超出容量之前所儲存的總人數?
int numLeaving = Integer.parseInt(JOptionPane.showInputDialog("number leaving"));
int numEntering = Integer.parseInt(JOptionPane.showInputDialog("number entering:"));
while (totalPeople <= 65 && totalPeople >= 0) {
try {
if (numLeaving >= 0 && numEntering >= 0) {
totalPeople = (totalPeople + numEntering) - numLeaving;
JOptionPane.showMessageDialog(null,"Total people = " + totalPeople);
}
else {
JOptionPane.showMessageDialog(null,"Invalid data");
}
}
catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null,"Enter numbers only");
}
if (totalPeople > 65) {
JOptionPane.showMessageDialog(null,"Capacity exceeded\n" + "Total people = " + totalPeople);
}
numLeaving = Integer.parseInt(JOptionPane.showInputDialog("number leaving"));
numEntering = Integer.parseInt(JOptionPane.showInputDialog("number entering:"));
}
不想打擾,但'break'或'System.exit'有什麼問題? –
@RC。看看這個:http://stackoverflow.com/questions/3922599/is-it-a-bad-practice-to-use-break-in-a-for-loop –
我完全同意'System.exit'但是在這種情況下,我沒有看到有任何'break'錯誤(並且恕我直言,'break'比loop-var = clean要乾淨一些。] –