方法没有返回正确的答案?有没有人有更多的输入?

时间:2015-02-03 19:53:17

标签: java

在这种方法中我应该归还选举的胜利者。这是通过重复使用candidatesWithFewest()方法完成的,直到只有一个候选人(投票最多)。您通过选票上的第一个名字获得投票。如果两个或两个以上的候选人拥有相同数量的第一名,那么选举就不具有决定性。我的错误是,当两个或更多候选人拥有相同数量的第一名选票时,它仍然会返回其中一个候选人。我的所有其他方法都返回正确的输出,所以我认为问题出在getWinner方法中。 candidatesWithFewest返回所有候选人最少的候选人名单。我想从列表中删除这些候选人,直到只剩下一名候选人。

    /**
    * 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
}
}

5 个答案:

答案 0 :(得分:0)

尝试使用candList.remove(s);代替candList.remove(listString);

另外,实际上就我而言,这个方法存在一些问题

public ArrayList<String> candidatesWithFewest(ArrayList<String> candidateList) {
    ArrayList<String> losers = new ArrayList<String>(); //empty list for losers
    int minTally = ballotList.size() + 1;      
    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; //.... here you have made minTally == votes
            losers = new ArrayList<String>(); // adds loser to list ..... here again you are repeating the first line of this method
        }
        if (votes == minTally) { //..... this will be true because although originally votes were less than minTally still, your assignment makes them equal
            losers.add(candidateList.get(can)); //adds losers with the same vote
        }
    }
    return losers;  // returns list of candidates with fewest votes
}

答案 1 :(得分:0)

我想代码片段:

for (String s : loser) {
            listString += s;
            candList.remove(listString);         
        } 

正在为您创建问题,因为它只适用于首次运行;对于其余的(n-1)运行它不会删除任何东西!

我无法理解您为什么要将候选名称加入listString变量? 如果您需要删除一个候选名称,那么以下应该是正确的方式:

for (String s : loser) {
            //listString += s;
            candList.remove(s);         
        } 

有道理吗?

答案 2 :(得分:0)

这是一个建议。如果您不喜欢它,请留下它!

解决此类问题的更明确的方法是使用散列映射 候选人姓名将是关键,价值是投票数。 当您遍历投票列表时,检查候选人的姓名是否存在于地图中,如果是,则只需递增计数,否则将其设置为1(这意味着您看到的第一个投票)

答案 3 :(得分:0)

我认为您的代码存在许多可能导致问题的问题。

for (String s : loser)
{
    listString += s;
    candList.remove(listString);
}

此代码将每个名称附加在输家列表中并尝试将其从candList中删除,因此假设输家列表中包含[joe,john,dave],那么该循环就是说删除(&#34; joe& #34;),然后删除(&#34; joejohn&#34;)然后删除(&#34; joejohndave&#34;) 这真的是你想要的吗?

int votes = numFirstVotes(candidate, candidateList);

这里可能存在问题,因为ballotList是全局的,也许你在这个函数中正确使用它,但是因为我看不到我不知道的代码。

由于我们没有numFirstVotes的代码或者如何设置ballotList,因此很难同意该代码有效。您的评论不会反映代码的作用,应该删除或更新。

我认为真正的问题在这里

while (candList.size() > 2)

这说,循环直到列表中有2个或更少的项目。它不会说循环,直到列表中有1个或更少的项目,根据您的评论是你想要它做的。 因此,如果没有其他代码来测试我的猜测,我无法确定,但我认为您希望将循环条件更改为

while (candList.size() >= 1)

如果在更改后仍然无法正常工作,那么您的代码可能会遇到更多问题,例如上面的for循环和numFirstVotes方法。

答案 4 :(得分:0)

我会对您的代码进行微小的修改。您可以直接从该方法返回获胜者,而不是返回失败者并迭代删除它们。基本上你通过删除每次迭代的min元素来找到max元素。这不是你应该接近的方式。

更改candidatesWithFewest方法candidatesWithHighest,并相应更改逻辑:

public ArrayList<String> candidatesWithHighest(ArrayList<String> candidateList) {
    ArrayList<String> winners = new ArrayList<String>(); //empty list for losers
    int maxTally = 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 > maxTally) {
            maxTally = votes;
            winners = new ArrayList<String>(); // Reset the list
        } else if (votes == maxTally) {
            winners.add(candidateList.get(can)); //adds winners with the same vote
        }
    }
    return winners;  // returns list of candidates with highest votes
}

然后你的第一个方法根本不需要任何while循环。只需获取列表,检查大小。如果它大于1,则抛出错误:

public String getWinner(VoterBallots vbal, ArrayList<String> candList) {
    ArrayList<String> winners = vbal.candidatesWithHighest(candList);

    if (candList.size() < 2) { 
        return candList.iterator().next(); // Return the surviving candidate
    } else {
        return "Election is non decisive.";
    }
}