不知道为什么,但是在第一次打印数据之后,它仅一次打印一张带有投票的名称,但是在修改之后,由于某种原因,它打印出了整个数组列表。
测试器类:
import java.util.ArrayList;
public class ElectionTesterV4 {
public static void main(String[] args) {
int sum = 0, counter = 0;
ArrayList<Candidate4> c = new ArrayList<Candidate4>();
Candidate4 John = new Candidate4("John Smith", 5000);
c.add(John);
Candidate4 Lucy = new Candidate4("Lucy Robertson", 8000);
c.add(Lucy);
Candidate4 Marie = new Candidate4("Marie Grace", 7000);
c.add(Marie);
Candidate4 Raymond = new Candidate4("Raymond Zhang", 10000);
c.add(Raymond);
Candidate4 JohnD = new Candidate4("John Doe", 2500);
c.add(JohnD);
Candidate4 JuanG = new Candidate4("Juan Garcia", 6000);
System.out.println("Raw Election Data: ");
System.out.println("--------------------------------");
for(Candidate4 ca : c) {
System.out.println(ca.toString());
sum += ca.getVotes();
counter++;
}
System.out.println();
System.out.println("Election Results");
System.out.println("--------------------------------");
System.out.println("Candidate Votes Received % of Total Votes");
for(Candidate4 ca : c) {
System.out.printf("%15s %5d %2.2f\n",
ca.getName(), ca.getVotes(), ((double)ca.getVotes() / sum) * 100);
}
System.out.println();
System.out.println("Total number of votes cast in election is: "+sum);
System.out.println();
John.replaceName(JuanG);
System.out.println();
sum = 0;
System.out.println("Raw Election Data: ");
System.out.println("--------------------------------");
for(Candidate4 ca : c) {
System.out.println(c.toString());
sum += ca.getVotes();
counter++;
}
System.out.println();
System.out.println("Election Results");
System.out.println("--------------------------------");
System.out.println("Candidate Votes Received % of Total Votes");
for(Candidate4 ca : c) {
System.out.printf("%15s %5d %2.2f\n",
ca.getName(), ca.getVotes(), ((double)ca.getVotes() / sum) * 100);
}
System.out.println();
System.out.println("Total number of votes cast in election is: "+sum);
System.out.println();
John.replaceVotes(6000);
System.out.println();
sum = 0;
System.out.println("Raw Election Data: ");
System.out.println("--------------------------------");
for(Candidate4 ca : c) {
System.out.println(c.toString());
sum += ca.getVotes();
counter++;
}
System.out.println();
System.out.println("Election Results");
System.out.println("--------------------------------");
System.out.println("Candidate Votes Received % of Total Votes");
for(Candidate4 ca : c) {
System.out.printf("%15s %5d %2.2f\n",
ca.getName(), ca.getVotes(), ((double)ca.getVotes() / sum) * 100);
}
System.out.println();
System.out.println("Total number of votes cast in election is: "+sum);
}
}
方法类:
public class Candidate4 {
// instance variables
private int numVotes;
private String name;
// Constructor for objects of class Candidate
public Candidate4(String name, int numVotes) {
// initialize instance variables
this.name = name;
this.numVotes = numVotes;
}
public String getName() {
return name;
}
public int getVotes() {
return numVotes;
}
public void setVotes(int n) {
numVotes = n;
}
public void setName(String n) {
name = n;
}
public String toString() {
return name + " received " + numVotes + " votes.";
}
public void replaceName(String n) { //replaces name
System.out.println("Changing " + name + "'s name to " + n);
name = n;
}
public void replaceVotes(int v) { //replaces votes)
System.out.println("Changing " + name + "'s votes to " + v);
numVotes = v;
}
public void replaceName(Candidate4 c) { //replaces name
System.out.println("Changing " + name + "'s name to " + c.getName());
name = c.getName();
}
public void replaceVotes(Candidate4 c) { //switches votes
System.out.println("Replacing " + name + "'s votes with " + c.getName());
numVotes = c.getVotes();
}
}
我已经使用类似的代码逐个对象地打印出arraylists,所以我不知道为什么这次它在循环中的每次迭代中都不断打印出整个arraylist。
答案 0 :(得分:0)
错误地放了:
System.out.println(c.toString());
代替
System.out.println(ca.toString());