2013-04-22 90 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); 
} 
}