简体   繁体   English

输出错误,我无法弄清原因

[英]Incorrect Output and I cannot figure out why

Code: 码:

    ArrayList<Employer> use = new ArrayList<Employer>();
    ArrayList<String> Alia = new ArrayList<String>();
    int moneyE, moneyF;
    String Alias = "";
    boolean alert, alertF;
    for(int i = 0; i < Filers.size(); i++)
    {
        moneyE=0;
        use.clear();
        Alia.clear();
        Alias = "";
        alert = false; alertF = false;
        use.addAll(this.findEmployerRecords(Filers.get(i)));
        moneyF = Filers.get(i).getIncome();

        for(int j = 0; j< use.size(); j++)
        {
            moneyE += use.get(j).getEmployeeWages();
            if(!(Filers.get(i).getName().equals(use.get(j).getEmployeeName())) 
            && !(Alia.contains(use.get(j).getEmployeeName())))
                Alia.add(use.get(j).getEmployeeName());
            if(moneyE !=moneyF 
            && !(Filers.get(i).getName().substring(0, Filers.get(i).getName().indexOf(" ")+1).equals(
             use.get(j).getEmployeeName().substring(0, use.get(j).getName().indexOf(" ")+1))))          //problem occurs here
                alert = true;
            if (alert = true)
                alertF = true;
        }
        for(int j=1; j<Alia.size();j++)
            Alias+= (", " + Alia.get(j));
        if (moneyE != moneyF)
            Discrepancies.add(new Discrepancy(alertF, moneyF-moneyE, Filers.get(i).getName(),
            Filers.get(i).getSSN(), Alias));
    }

I am at the end of my stick. 我在坚持到底。 I cannot figure out what I am doing wrong here. 我无法弄清楚我在做什么错。 This is a bit of the code I am working with to create a virtual IRS. 这是我用来创建虚拟IRS的一部分代码。 If I remove the +1 at the problem spot, I get an out of bound exception. 如果我在有问题的地方删除+1,我将遇到异常。 The problem I am having is with the alert and alertF. 我遇到的问题是alert和alertF。 The alert status should go off if the moneyE and moneyF are mismatched AND the last name is spelled different between the employer records and the filer. 如果moneyE和moneyF不匹配,并且雇主记录和申报人的姓氏拼写不同,则警报状态应处于关闭状态。 Alerts are shown with the asterisks. 警报以星号显示。

Expected: 预期:

  *-100, HEATON MARK, 309582302, MCGUIRE MARK
  *-50, FOX CHARLES, 724113610, BOX CHARLES
   +105, MOFFITT DONALD, 206516583, MOFFITT DON
   +100, YOUNG THOMAS, 813068590, YOUNG THOM, YOUNG TOM
   -20, SOMASUNDAR PRASANTH, 138001926
   +5, HORSLEY MARIA, 239984300
   +5, LIANG BO, 743287509
   +5, LIANG BO, 857410861

Actual: 实际:

  *105, MOFFITT DONALD, 206516583
  *-100, HEATON MARK, 309582302, HEATON MARK
  *100, YOUNG THOMAS, 813068590, YOUNG TOM
  *-50, FOX CHARLES, 724113610
  *-20, SOMASUNDAR PRASANTH, 138001926
  *5, HORSLEY MARIA, 239984300
  *5, LIANG BO, 743287509
  *5, LIANG BO, 857410861

Simplify your code by taking out the repeated calls to the same object, ie you have numerous calls to Filers.get(i) within your logic. 通过删除对同一对象的重复调用来简化代码,即,在逻辑内多次调用Filers.get(i) Abstract these out to improve the readability of the code. 将这些内容抽象出来以提高代码的可读性。

Also you have lots of not statements, think about your logic. 另外,您还有很多not语句,请考虑一下您的逻辑。

Hint: !A & !B = ! (A | B) 提示: !A & !B = ! (A | B) !A & !B = ! (A | B)

Below is a simplified, easier to read version of your code. 以下是简化,易于阅读的代码版本。

alert = false; 
alertF = false;
Filer f = Filers.get(i);
use.addAll(this.findEmployerRecords(f));
moneyF = f.getIncome();
String filersName = f.getName();

for(int j = 0; j< use.size(); j++)
{
    moneyE += use.get(j).getEmployeeWages();
    String emplName = use.get(j).getEmployeeName();

    if(!(filersName.equals(emplName) || Alia.contains(emplName)))
            Alia.add(emplName );

    if(!(moneyE == moneyF || filersName.substring(0, filersName.indexOf(" ")+1).equals(
         emplName.substring(0, use.get(j).getName().indexOf(" ")+1))))  //problem occurs here
            alert = true;

    if (alert = true)
            alertF = true;
}
for(int j=1; j<Alia.size();j++)
     Alias+= (", " + Alia.get(j));
if (moneyE != moneyF)
     Discrepancies.add(new Discrepancy(alertF, moneyF-moneyE, filersName, f.getSSN(), Alias));

To help you understand what is going wrong, add some print statements like this 为了帮助您了解问题所在,请添加一些打印语句,如下所示

System.out.println("i=" + i + ", j=" + j + ", fName="+ filersName + ", index= "+ filersName.indexOf(" ") + ", substing= "+ filersName.substring(0, filersName.indexOf(" ")+1)); 

before your logic statements. 在逻辑语句之前。 Also add one for employer name that you are comparing. 还要为要比较的雇主名称添加一个。


There is also this one big problem that this line here 还有一个大问题,这条线在这里

if (alert = true)

is ALWAYS setting the alert variable to true - not comparing, as you only have a single = . 总是将alert变量设置为true-不进行比较,因为只有一个= In fact you do not need to compare booleans in logic statements 实际上,您不需要在逻辑语句中比较布尔值

if (A == true) is equivalent to if(A) if (A == true)等同于if(A)

even better is you can just have a single boolean. 更好的是,您可以只拥有一个布尔值。 Your code is like this 您的代码是这样的

 if(/*Condition*/)
      alert = true;
 if (alert)
      alertF = true;

and can be replaced with just 可以替换为

 if(/*Condition*/)
      alertF = true;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM