-3
每次我嘗試運行此代碼時currentCost
返回爲0
。Java:循環不工作
我試圖計算折舊@ 20%使用循環從用戶的資產價值和年齡(這裏是汽車)。謝謝。
float cost = Integer.parseInt(jTextField2.getText());
int vehicalAge = Integer.parseInt(jTextField3.getText());
float currentCost = 0;
for (int a = 1; a == vehicalAge; a++) {
cost = (float) (cost - (cost * 0.2));
currentCost = cost;
}
JOptionPane.showMessageDialog(this, Float.toString(currentCost));
'一個== vehicalAge'應該是'一個<= vehicalAge' – Eran
這是通過步進您的調試器中的代碼將有所幫助。 –