我创建了一个程序,该程序读取文本文件并为候选人创建一个数组,然后为每个选民创建一个对象以及一个用于他们偏好的数组。我遇到麻烦的方法是投票方法。要赢得候选人,他们必须获得超过一半的选票。因此,一种方法检查它们是否有效,然后删除具有最低投票数的候选人,我遇到的问题是删除它之后我不知道如何检查他们的下一个偏好。
这是我投票的方法
public static int[] doVoting(Voter[] voters, String[] cand){
// Create an int array to store votes
int[] votes= new int[cand.length];
//The first string of the candidates array will correspond to the first number in the int array
for (int i=0; i< voters.length; i++){
for (int j=0; j< cand.length; j++){
for (int k=0;k< voters[i].preferences.length; k++){
if(voters[i].preferences[k].equals(cand[j]))
{votes[j]= votes[j] + 1;
}
else
break;
}
}
}
return votes;
}
这是检查投票并做出最终决定的地方
public static void doAlternativeVoteElection(Voter[] voters, String[] candidate)
{
int[] votes = doVoting(voters,candidate);
int max = votes[0];
int min = votes[0];
int sumVote = 0;
int elemax=0;
int elemin=0;
for (int i=0;i<votes.length; i++){
sumVote += votes[i];
if (votes[i] > max){
max=votes[i];
elemax = i;
}
if(votes[i] <= min){
elemin = i;
min= votes[i];
}
}
while (max <= sumVote/2)
{
candidate = arrayDel(elemin, candidate);
System.out.println("he");
votes= arrayDelInt(elemin, votes);
votes = doVoting(voters, candidate);
for (int i=0; i<candidate.length;i++){
System.out.println(candidate[i]);
System.out.println(votes[i]);
if (votes[i]> max){
max=votes[i];
elemax = i;
}
else if(votes[i] <= min){
elemin = i;
min= votes[i];
}
else
continue;
}
if(max > sumVote/2)
break;
}
if(max > sumVote/2)
System.out.println(candidate[elemax]+" wins");
}
public static int[] arrayDelInt(int min, int[] array)
{
int[] retva = new int[array.length-1];
for (int i=0; i<min; i++)
retva[i] = array[i];
for (int i=min+1; i<array.length; i++)
retva[i-1] = array[i];
return retva;
}
问题是doVoting方法在一个候选人被淘汰后没有将投票添加到下一个偏好。所以最终arrayDel方法只删除所有元素。
这是arrayDel方法
public static int[] arrayDelInt(int min, int[] array)
{
int[] retva = new int[array.length-1];
for (int i=0; i<min; i++)
retva[i] = array[i];
for (int i=min+1; i<array.length; i++)
retva[i-1] = array[i];
return retva;
}
对于arrayDelInt
是一样的答案 0 :(得分:0)
你可能有两个潜在的错误。
while (max <= sumVote/2)
&lt; ---整数除法将导致sumVote/2
向下舍入。请改用sumVote / 2.0
。这很可能不是,但无论如何应该被视为潜在的错误。
在doVoting的此代码中,根据您声明的要求,相信破坏声明应在候选人的投票计数增加后发生。尝试将最后一个if块更改为如下所示
if(voters[i].preferences[k].equals(cand[j])) {
votes[j]= votes[j] + 1;
break;
}
答案 1 :(得分:0)
我认为最好重新考虑你的设计是否更加面向对象,比如; (请考虑这个伪代码)
public class Candidate {
// maps the preference i.e. 1st, 2nd, etc. to votes cast
private HashMap<String, int> votes = new HashMap<String, int>();
public void addFirstPref() {
votes.put("first", votes.get("first") + 1);
}
// and so on for addSecondPref() etc
}
public class Ballot {
// maps preference to candidate
HashMap<String, String> votes = new HashMap<String, String>();
public String getFirstPref() {
return votes.get("first");
}
// and so on for getSecondPref() etc.
}
public class Election {
// populate a collection of candidates and ballots
public void doVoting() {
// count all the ballots
for (Ballot ballot : ballotsColn) {
candidatesColn.get(ballot.getFirstPref()).addFirstPref();
// repeat for all preferences
}
// now do rounds until there's one left
while (candidatesColn.size() > 1) {
// find the candidate with the lowest first preferences
// find all the ballots where that candidate is the first pref
// iterate all those ballots, find the second pref candidate
// add 1 vote to the first pref count of that candidate
// remove the candidate from the collection
}
}
}