2013-04-22 88 views
1

在我的代碼循環中,如果給定的oldsalary [i]不符合這些準則,我想將舊數值oldsalary [i]恢復爲「錯誤」。不過,我希望它保持原樣[我],因爲我將在後面的代碼中顯示所有oldsalary [i]。將雙數值轉換爲文本

所以基本上當所有的oldsalary [i]都顯示在另一個循環中時,我希望能夠看到「Error」,所以知道這個值有什麼問題。

我知道我擁有它的方式是完全錯誤的,我只是把它說成是有道理的。對不起,如果它沒有任何意義。

if(oldsalary[i] < 25000 || oldsalary[i] > 1000000){ 

     JOptionPane.showMessageDialog(null, userinput[i]+"'s salary is not within 
     necessary limit.\n Must be between $25,000 and $1,000,000. \n If salary is 
     correct, empolyee is not eligible for a salary increase."); 

     double oldsalary[i] = "Error"; 





     } 
+1

你可以只設置'oldsalary [i] = Double.MIN_VALUE'再後來,當你打印出來,檢查'如果(工資[I] == Double.MIN_VALUE){/ * error * /} else {/ *正常打印* /}'。或者只是使用有效範圍之外的任何值作爲「錯誤」值。 – Supericy 2013-04-22 20:04:08

回答

2

不能同時存儲該數值在單個double值的誤差指示器。

你最好的賭注是包裹工資作爲一個同時包含薪水值和指示錯誤條件的布爾對象:

class Salary { 
    private double value; 
    private boolean error = false; 
    ... constructor, getters and setters 
} 

和更新您的代碼使用對象來代替。即

if(oldsalary[i].getValue() < 25000 || oldsalary[i].getValue() > 1000000) { 
    oldsalary[i].setError(true); 
    ... 
} 

所以後來你可以做

if (oldsalary[i].isError()) { 
    // display error message 
} 
0

您可以使用額外的List來存儲沒有通過您的需求測試的索引。

List<Integer> invalidIndices = new ArrayList<>(); 
for (...){ 

if(oldsalary[i] < 25000 || oldsalary[i] > 1000000){ 

     JOptionPane.showMessageDialog(null, userinput[i]+"'s salary is not within 
     necessary limit.\n Must be between $25,000 and $1,000,000. \n If salary is 
     correct, empolyee is not eligible for a salary increase."); 

     invalidIndices.add(i); 
} 
}