简体   繁体   English

遍历ArrayList-不打印任何内容

[英]Iterating through ArrayList - not printing anything

So i need to create an ArrayList that will only take candidates who's... 所以我需要创建一个ArrayList,只接受那些...

  • 'grade' is less than 85%, “等级”低于85%,
  • if 'grade' is less than 85%, test their 'regulation' 如果“等级”低于85%,请测试其“规定”
  • if their 'regulation' >= to 0.5, test their 'communication' 如果他们的“调节”> = 0.5,请测试他们的“沟通”
  • if their 'communication' equals 'average' or 'excellent' 如果他们的“交流”等于“平均”或“优秀”

Then add it to the list 然后将其添加到列表中

I THOUGHT thats what my code was doing, but its not printing anything. 我想这就是我的代码在做什么,但它没有打印任何内容。 Since its not coming up with any errors, it leads me to believe that its not really iterating through it. 由于它没有出现任何错误,因此使我相信它并没有真正遍历它。 Or its iterating through the first one (which doesn't meet the criteria) and stops, so it won't have anything to print. 或者它遍历第一个(不符合条件)并停止,因此将没有任何可打印的内容。 Ive looked up other examples and tried implementing and changing a few things but its still not working. Ive查找了其他示例,并尝试实现和更改一些内容,但仍然无法正常工作。 To me it looks like it should work so i cant figure out what to change. 对我来说,它似乎应该工作,所以我不知道要更改什么。

Any suggestions would be seriously appreciated! 任何建议将不胜感激!

import java.util.ArrayList;

public class Candidate extends AddressBook {

  private boolean innovation;
  private double grade;
  private double regulation;
  private String communication;

  public Candidate(String fn, String ln, double grade, String comm,
          boolean innov, double reg) {
      super(fn, ln);

      this.grade = grade;
      this.communication = comm;
      this.innovation = innov;
      this.regulation = reg;
}

  public boolean isInnovative() {
      return innovation;
}

  public double getGrade() {
      return grade;
}

  public double getRegulation() {
      return regulation;
}

  public String getCommunication() {
      return communication;
}

public static ArrayList<Candidate> getEligibleCandidates(Candidate[] cands) {
    ArrayList<Candidate> eligibleCandidates = new ArrayList<Candidate>();

    Candidate person = cands[0];

    for (Candidate i : cands) {
        while (i.getGrade() < 85) {
            if (i.getRegulation() >= 0.5) {
                if (i.getCommunication().equals("average")
                        || i.getCommunication().equals("excellent")) {

                    person = i;
                    eligibleCandidates.add(i);

                }
            }

        }

    }

    return eligibleCandidates;
}

public void setCommunication(String comm) {
    this.communication = comm;
}

public void setGrade(double grade) {
    this.grade = grade;
}

public void setInnovation(boolean innov) {
    this.innovation = innov;
}

public void setRegulation(double reg) {
    this.regulation = reg;
}



public static void main(String[] args) {

    Candidate r1 = new Candidate("Elena", "Brandon", 82.30, "poor", true,
            0.5);
    Candidate r2 = new Candidate("Thomas", "Molson", 85.10, "poor", false,
            1.0);
    Candidate r3 = new Candidate("Hamilton", "Winn", 77.77, "average",
            false, 0.8);
    Candidate r4 = new Candidate("Suzie", "Sarandin", 69.93, "average",
            false, 0.0);
    Candidate r5 = new Candidate("Philip", "Winne", 93.03, "average", true,
            1.0);
    Candidate r6 = new Candidate("Alex", "Trebok", 88.61, "poor", true, 0.7);
    Candidate r7 = new Candidate("Emma", "Pivoto", 55.99, "excellent",
            false, 0.8);
    Candidate r8 = new Candidate("John", "Lenthen", 87.49, "excellent",
            true, 0.9);
    Candidate r9 = new Candidate("James", "Lean", 88.00, "excellent",
            false, 0.5);
    Candidate r10 = new Candidate("Jane", "Ostin", 91.20, "average", true,
            0.6);
    Candidate r11 = new Candidate("Emily", "Car", 66.79, "excellent",
            false, 0.3);
    Candidate r12 = new Candidate("Daniel", "", 76.65, "average", true, 0.2);
    Candidate r13 = new Candidate("Neda", "Bazdar", 55.89, "excellent",
            true, 0.5);
    Candidate r14 = new Candidate("Aaron", "Smith", 90.01, "excellent",
            false, 0.3);
    Candidate r15 = new Candidate("Kate", "Hen", 87.9, "poor", false, 0.8);

    Candidate[] cands = { r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, r11,
            r12, r13, r14, r15 };

    ArrayList<Candidate> people = Candidate.getEligibleCandidates(cands);

    System.out.println(people);

}

}

It stops because 它停止了,因为

for (Candidate i : cands) {
    while (i.getGrade() < 85) {
        if (i.getRegulation() >= 0.5) {
            if (i.getCommunication().equals("average")
                    || i.getCommunication().equals("excellent")) {
                person = i;
                eligibleCandidates.add(i);
            }
        }
    }
}

Think about it once it enters while (i.getGrade() < 85) it never changes the value for the condition to break out of the loop. 考虑一下它一旦进入(i.getGrade()<85),它就不会更改条件的值以使其脱离循环。 This will be an endless for loop. 这将是无穷无尽的for循环。 you need to change that to an if statement 您需要将其更改为if语句

for (Candidate i : cands) {
    if (i.getGrade() < 85) {
        if (i.getRegulation() >= 0.5) {
            if (i.getCommunication().equals("average")
                    || i.getCommunication().equals("excellent")) {
                person = i;
                eligibleCandidates.add(i);
            }
        }
    }
}

Once your code enters while (i.getGrade() < 85) it will never exit. 一旦您的代码进入while (i.getGrade() < 85)它就永远不会退出。 That should be an if check, not a loop. 那应该是一个if检查,而不是一个循环。 You never change the person you are checking against while inside that loop , so it will continue to be true forever once it is true once. 在该loop内,您永远不会更改要检查的人,因此一旦它成为真,它将永远永远为真。

For example, with "Elena", the loop will execute and check is Elena's grade < 85? 例如,使用“ Elena”,将执行循环并检查is Elena's grade < 85? . Since it is, it will enter the loop. 既然如此,它将进入循环。 Then, once the loop finishes processing, it will go back to the top of the loop and check, is Elena's grade STILL less than 85? 然后,一旦循环完成处理,它将返回循环顶部并检查, is Elena's grade STILL less than 85? , which will be true because it never changed in the loop. ,这是正确的,因为它从未在循环中更改过。

You should use an if check, like below. 您应该使用if检查,如下所示。

    for (Candidate currentCandidate : cands) {
        if(currentCandidate.getGrade()<85 && currentCandidate.getRegulation() >= 0.5 && (currentCandidate.getCommunication().equals("average") || currentCandidate.getCommunication().equals("excellent"))){
            eligibleCandidates.add(currentCandidate);
        }
    }

It should be: 它应该是:

if ((i.getGrade() < 85) &&
    (i.getRegulation() >= 0.5) &&
    (i.getCommunication().equals("average") || i.getCommunication().equals("excellent"))) {
        person = i;
        eligibleCandidates.add(i);
    }
  }
}

...and not while ... ...而不是while ...

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

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