gpt4 book ai didi

java - 编辑后继续打印整个数组列表

转载 作者:太空宇宙 更新时间:2023-11-04 09:49:11 25 4
gpt4 key购买 nike

不知道为什么,第一次打印数据后,只一一打印出带有投票的名字,但修改后,由于某种原因,它打印出了整个数组列表。

测试人员类别:

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

我已经使用与此类似的代码逐个对象地打印出数组列表,所以我不知道为什么这次它在循环中的每次迭代中继续打印出整个数组列表。

最佳答案

错误地输入了:

System.out.println(c.toString());

而不是

System.out.println(ca.toString());

关于java - 编辑后继续打印整个数组列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55015243/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com