2016-02-29 91 views
0

工資的值未在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); 
    } 
} 
+3

在循環結束時,您是否應該不將變量'ann'存儲回工資中,因爲這將是他們的新薪水? – Kon

回答

1

那是因爲你沒有每年更新薪水,所以每年的結果都是一樣的。嘗試每年更新工資或將工資保存在其他變量中。

這裏有一個修改代碼:

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); 
     salary = perc + salary; 
     JOptionPane.showMessageDialog(null, "Annual salary for employee " +i +" for the year " +y +" is €"+salary); 
    } 
} 
+0

謝謝你。我現在可以看到我的錯誤:) – hDDen

+0

@PatrykMichta很樂意幫助:D – Leandro

0

salary沒有更新,因爲從來沒有在任何一種蓄電池的使用...

你在這裏定義:

double salary = Double.parseDouble(JOptionPane.showInputDialog("Please enter the salary for employee "+i)); 

之後,你正在閱讀它,但從來沒有改寫它..

0

你的代碼中的小修改:

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; 
     salary = ann; 
     JOptionPane.showMessageDialog(null, "Annual salary for employee " +i +" for the year " +y +" is €"+ann); 
    } 
} 
+0

謝謝你的幫助:) – hDDen

0

添加

salary=ann; 

後:

JOptionPane.showMessageDialog(null, "Annual salary for employee " +i +" for the year " +y +" is €"+ann); 

修改代碼:

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); 
     salary=ann; 
    } 
} 

希望得到您的解決方案。

+0

謝謝你的幫助:) – hDDen