迭代通过ArrayList - 不打印任何内容

时间:2015-11-16 20:56:36

标签: java arraylist

所以我需要创建一个只接受候选人的ArrayList ...

  • '等级'低于85%,
  • 如果'等级'低于85%,请测试他们的'规则'
  • 如果他们的'规则'> = 0.5,请测试他们的'沟通'
  • 如果他们的'沟通'等于'平均'或'优秀'

然后将其添加到列表

我认为这是我的代码所做的,但它不打印任何东西。由于它没有出现任何错误,它让我相信它并没有真正地反复出现。或者它遍历第一个(不符合标准)并停止,因此它将无法打印任何内容。我查了一些其他的例子,尝试实现和改变一些东西,但它仍然无法正常工作。对我来说,它看起来应该工作,所以我无法弄清楚要改变什么。

任何建议都会受到重视!

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

}

}

3 个答案:

答案 0 :(得分:1)

因为

而停止
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);
            }
        }
    }
}

一旦进入而考虑它(i.getGrade()&lt; 85)它永远不会改变条件的值以摆脱循环。这将是一个无休止的循环。你需要将其改为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);
            }
        }
    }
}

答案 1 :(得分:0)

一旦您的代码输入while (i.getGrade() < 85),它就永远不会退出。那应该是if检查,而不是循环。你永远不会在loop内部改变你正在检查的人,所以一旦真实,它将永远是真的。

例如,对于“Elena”,循环将执行并检查is Elena's grade < 85?。既然如此,就会进入循环。然后,一旦循环完成处理,它将返回到循环的顶部并检查is Elena's grade STILL less than 85?,这将是真的,因为它在循环中从未改变。

您应该使用if支票,如下所示。

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

答案 2 :(得分:0)

应该是:

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

...而不是while ...