工資的值未在for
循環中更新。如果我輸入的薪水是17000,那麼第一年的百分比是5%,我得到17850的輸出是正確的。然後循環再次返回,我再次輸入例如5%,我再次得到17850的輸出。我預計它會再增加5%來獲得18742.5。For循環,整數值未更新
int n = Integer.parseInt(JOptionPane.showInputDialog("Please enter the amount of employees"));
int yr = Integer.parseInt(JOptionPane.showInputDialog("Please enter the amount of years"));
for (int i = 1; i <=n; i ++) {
double salary = Double.parseDouble(JOptionPane.showInputDialog("Please enter the salary for employee "+i));
for (int y = 1; y <= yr; y++) {
double percentage = Double.parseDouble(JOptionPane.showInputDialog("Please enter the percentage for year " +y));
double perc =(salary * percentage/100);
double ann = perc + salary;
JOptionPane.showMessageDialog(null, "Annual salary for employee " +i +" for the year " +y +" is €"+ann);
}
}
在循環結束時,您是否應該不將變量'ann'存儲回工資中,因爲這將是他們的新薪水? – Kon