1

在我的代码循环中的以下 if 语句中,如果给定的 oldsalary[i] 不符合这些准则,我想将 oldsalary[i] 的先前数值恢复为“错误”。但是我希望它保留为 oldsalary[i],因为稍后我将在我的代码中显示所有 oldsalary[i]。

所以基本上当所有 oldsalary[i] 都显示在另一个循环中时,我希望能够看到“错误”,所以它知道该值有问题。

我知道我拥有它的方式是完全错误的,我只是这样说才有意义。对不起,如果它没有任何意义。

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";





        }
4

2 回答 2

2

您不能将数值错误指示符存储在一个double值中。

最好的办法是将薪水包装为一个对象,该对象同时包含薪水值和指示错误条件的布尔值:

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

并更新您的代码以改用该对象。IE

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

所以以后你可以做

if (oldsalary[i].isError()) {
    // display error message
}
于 2013-04-22T20:05:52.253 回答
0

您可以使用一个额外的列表来存储未通过您的需求测试的索引。

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);
}
}
于 2013-04-22T20:05:50.560 回答