【发布时间】:2015-02-03 19:53:16
【问题描述】:
在这种方法中,我应该返回选举的获胜者。这是通过重复使用candidatesWithFewest() 方法来完成的,直到只有一个候选人(得票最多)。您通过成为选票上的名字获得选票。如果两名或多名候选人获得相同数量的第一名选票,则选举不是决定性的。我的错误是,当两个或多个候选人拥有相同数量的第一名选票时,它仍然会返回其中一个候选人。我所有的其他方法都返回正确的输出,所以我相信问题出在getWinner 方法中。
CandidateWithFewest 返回所有得票最少的候选人的列表。我想从名单中删除这些候选人,直到只剩下一名候选人。
/**
* Returns the winner of the election using the candidatesWithFewest()
* method.If there is no winner method returns a statement stating the
* election is not decisive.
*
* @param vbal VoterBallots object
* @param candList a list of candidate names
* @return the winner of the election
*/
public String getWinner(VoterBallots vbal, ArrayList<String> candList) {
// Run rounds until down to a single candidate
while (candList.size() > 2) {
ArrayList<String> loser = vbal.candidatesWithFewest(candList);
String listString = "";
for (String s : loser) {
listString += s;
candList.remove(listString);
}
}
if (candList.size() > 0) {
return candList.iterator().next(); // Return the surviving candidate
} else {
return "Election is non decisive.";
}
}
/**
* Returns a list of one or more candidates tied with the fewest
* first choice votes
*
* Precondition: each String in candidateList appears exactly once
* in each Ballot in ballotList
*
* @param candidateList a list of candidate names
*
* @return a list of those candidates tied with the fewest first
* choice votes
*/
public ArrayList<String> candidatesWithFewest(ArrayList<String> candidateList) {
ArrayList<String> losers = new ArrayList<String>(); //empty list for losers
int minTally = ballotList.size() + 1; //number of min votes
for (int can = 0; can < candidateList.size(); can++) {
String candidate = candidateList.get(can);
// // number of first place votes
int votes = numFirstVotes(candidate, candidateList);
if (votes < minTally) {
minTally = votes;
losers = new ArrayList<String>(); // adds loser to list
}
if (votes == minTally) {
losers.add(candidateList.get(can)); //adds losers with the same vote
}
}
return losers; // returns list of candidates with fewest votes
}
/**
* Returns the number of times a given candidate appears first, among those
* elements that are on candidateList, among all elements of ballotList
* (i.e., among all ballots)
*
* @param candidate the name of a candidate
* @param candidateList a list of candidate names Precondition: candidate
* appears in candidateList
* @return the number of times that candidate is first among those in
* candidateList for all elements of ballotList
*/
public int numFirstVotes(String candidate, ArrayList<String> candidateList)
// implementation not shown
{
int numVotes = 0;
for (Ballot voterBallot : ballotList) {
String first = voterBallot.firstChoiceFrom(candidateList);
if (candidate.equals(first)) {
numVotes++;
}
}
return numVotes;
}
/**
* @param candidateList a list of candidate names
* @return the name of the first choice candidate for this Ballot from those
* in candidateList
*/
public String firstChoiceFrom(ArrayList<String> candidateList) {
for (String firstChoice : ballot) {
if(candidateList.contains(firstChoice))
{
return firstChoice;
}
}
return null; // does not appear on candidate list
}
}
【问题讨论】:
-
需要更多细节吗?
-
你可能只想要
candList.remove(s)我认为因为你一直将失败者附加到listString上,这可能不是你想要删除的失败者的名字,它会是所有的名字,因此candList.remove会失败 -
不应该是
candList.remove(listString);是candList.remove(s);? -
losers = new ArrayList<String>(); // adds loser to list代码与评论不符,你确定这是你想做的吗? -
如果我这样做 candList.remove(s) 我总是得到“选举不是决定性的”。
标签: java