为什么我的程序只打印出数组的最后一个对象值?
Why is my program printing out only the last object value of the array?
候选人class:
public class Candidate
{
private static String name;
private static int numVotes;
Candidate(String name, int numVotes)
{
Candidate.name = name;
Candidate.numVotes = numVotes;
}
public String toString()
{
return name + " recieved " + numVotes + " votes.";
}
public static int getVotes()
{
return numVotes;
}
public static void setVotes(int inputVotes)
{
numVotes = inputVotes;
}
public static String getName()
{
return name;
}
public static void setName(String inputName)
{
name = inputName;
}
}
应试者class:
public class TestCandidate
{
public static Candidate[] election = new Candidate[5];
public static void addCandidates(Candidate[] election)
{
election[0] = new Candidate("John Smith", 5000);
election[1] = new Candidate("Mary Miller", 4000);
election[2] = new Candidate("Michael Duffy", 6000);
election[3] = new Candidate("Tim Robinson", 2500);
election[4] = new Candidate("Joe Ashton", 1800);
}
public static int getTotal(Candidate[] election)
{
int total = 0;
for (Candidate i : election)
{
total += Candidate.getVotes();
}
return total;
}
public static void printResults(Candidate[] election)
{
System.out.printf("%s%12s%25s", "Candidate", "Votes", "Percentage of Votes\n");
for (Candidate i: election)
{
System.out.printf("\n%s%10s%10s", Candidate.getName(), Candidate.getVotes(), ((double)Candidate.getVotes()/getTotal(election) * 100));
}
System.out.println("\n\nTotal Number of Votes: " + getTotal(election));
}
public static void main (String args[])
{
addCandidates(election);
printResults(election);
}
}
每当我 运行 TestCandidate class,它输出这个:
Candidate Votes Percentage of Votes
Joe Ashton 1800 20.0
Joe Ashton 1800 20.0
Joe Ashton 1800 20.0
Joe Ashton 1800 20.0
Joe Ashton 1800 20.0
Total Number of Votes: 9000
程序的重点是输出所有候选人并根据每个人计算平均值。我相信这是我的 for-each 循环中的一个问题。如有任何帮助,我们将不胜感激。
private static String name;
private static int numVotes;
静态成员对 class 的所有实例都有一个值。删除 static 关键字以使不同的实例具有不同的值。
如果你这样做:
private static String name;
private static int numVotes;
您创建的每个新 Candidate
对象将覆盖所有其他对象的字段 name
和 numVotes
,因为这些字段不是对象字段是 class 字段。 .
您需要删除这些字段的静态关键字才能工作:
示例:
public class Candidate
{
private String name;
private int numVotes;
Candidate(String name, int numVotes)
{
Candidate.name = name;
Candidate.numVotes = numVotes;
}
.... .... ETC ETC
候选人class:
public class Candidate
{
private static String name;
private static int numVotes;
Candidate(String name, int numVotes)
{
Candidate.name = name;
Candidate.numVotes = numVotes;
}
public String toString()
{
return name + " recieved " + numVotes + " votes.";
}
public static int getVotes()
{
return numVotes;
}
public static void setVotes(int inputVotes)
{
numVotes = inputVotes;
}
public static String getName()
{
return name;
}
public static void setName(String inputName)
{
name = inputName;
}
}
应试者class:
public class TestCandidate
{
public static Candidate[] election = new Candidate[5];
public static void addCandidates(Candidate[] election)
{
election[0] = new Candidate("John Smith", 5000);
election[1] = new Candidate("Mary Miller", 4000);
election[2] = new Candidate("Michael Duffy", 6000);
election[3] = new Candidate("Tim Robinson", 2500);
election[4] = new Candidate("Joe Ashton", 1800);
}
public static int getTotal(Candidate[] election)
{
int total = 0;
for (Candidate i : election)
{
total += Candidate.getVotes();
}
return total;
}
public static void printResults(Candidate[] election)
{
System.out.printf("%s%12s%25s", "Candidate", "Votes", "Percentage of Votes\n");
for (Candidate i: election)
{
System.out.printf("\n%s%10s%10s", Candidate.getName(), Candidate.getVotes(), ((double)Candidate.getVotes()/getTotal(election) * 100));
}
System.out.println("\n\nTotal Number of Votes: " + getTotal(election));
}
public static void main (String args[])
{
addCandidates(election);
printResults(election);
}
}
每当我 运行 TestCandidate class,它输出这个:
Candidate Votes Percentage of Votes
Joe Ashton 1800 20.0
Joe Ashton 1800 20.0
Joe Ashton 1800 20.0
Joe Ashton 1800 20.0
Joe Ashton 1800 20.0
Total Number of Votes: 9000
程序的重点是输出所有候选人并根据每个人计算平均值。我相信这是我的 for-each 循环中的一个问题。如有任何帮助,我们将不胜感激。
private static String name;
private static int numVotes;
静态成员对 class 的所有实例都有一个值。删除 static 关键字以使不同的实例具有不同的值。
如果你这样做:
private static String name;
private static int numVotes;
您创建的每个新 Candidate
对象将覆盖所有其他对象的字段 name
和 numVotes
,因为这些字段不是对象字段是 class 字段。 .
您需要删除这些字段的静态关键字才能工作:
示例:
public class Candidate
{
private String name;
private int numVotes;
Candidate(String name, int numVotes)
{
Candidate.name = name;
Candidate.numVotes = numVotes;
}
.... .... ETC ETC