2011-04-21 31 views
1

...当一个数组中的分数按降序重新排列时,另一个数组对分数具有正确的名称,例如,当悬崖得分1,戴夫得分2,埃里克得分3,鲍勃得分4,安迪得分5 ......得分表应该看起来像这样(不是格式化的,因为它将在输出中显示我需要发生的数据。) :安迪5,鲍勃4,埃里克3,戴夫2,克里夫1或至少这是如何安排名字,以便他们保持在相同的顺序/配对与他们的分数作为分数出来。你怎么能这样做?到目前为止,这是完整的程序,我刚才关注的部分是两个方法:“排序”。我想这只是我对交换或排序功能如何工作的理解。我只关心排序和打印数组,剩下的只是显示更多的程序应该做的事情。如何让两个数组按照完全相同的方式排序,以便

所有帮助,将不胜感激:)

import java.util.Scanner; 
public class InThirdPlace { 
    public static void main(String[] args) { 
     Scanner input = new Scanner(System.in); 
     String candidates[] = {"cliff", "Dave", "Eric", "Bob", "Andy"}; 
     int votes [] = new int [5]; 
     int vote = 0; 
     System.out.println("please enter candidate no. (0-4) you wish to vote for"); 
     vote = input.nextInt(); 
     while (vote != -1){ 
      if (vote >= 0 && vote <= votes.length - 1){ 
       votes[vote] ++ ;} 
     else{ 
      System.out.println("please enter a valid value.");} 
      System.out.println("Enter a vote (0 -4)"); 
      vote = input.nextInt();} 
     sort(votes, candidates); 
     printArray(candidates, votes); 
     System.out.println("3rd "+ candidates[2]); 
      System.out.println("in 2nd "+ candidates [1]); 
      System.out.print("1st " + candidates [0]); 
} 

public static void sort(int votes[], String names[]){ 

    for (int i = votes.length; i>=2; i--){ 

     int largestIndex = findPositionOfBiggest(votes,i); 

     swap(votes, i-1, largestIndex); 
     swapNames(names, i-1, largestIndex); 
    } 
} 


public static int findPositionOfBiggest(int votes[], int numItems){ 
    int indexLargest=0;//Set the largest index to 0 

    for(int loop=1;loop<numItems;loop++){ 

     if (votes[loop]>votes[indexLargest]){ 

      indexLargest = loop; 
     } 
    } 
    return indexLargest; 
} 

public static void swap(int votes[], int pos1, int pos2){ 

    int temp = votes[pos1]; 
    votes[pos1] = votes[pos2]; 
    votes[pos2] = temp; 
} 

public static void swapNames(String names[], int pos1, int pos2){ 

    String temp = names[pos1]; 
    names[pos1] = names[pos2]; 
    names[pos2] = temp; 
} 
public static void printArray(String names[], int votes[]){ 
    for (int i=0; i < votes.length; i++){ 
     System.out.println(names[i] + votes[i] + " "); 
    } 
    System.out.println(); 
} 
} 

回答

3

为什么不把你的阵列将创建一个对象的数组?然后,您可以将名称和分数绑定在一起,并且在对数组进行排序时,名称已与其关联。

+0

另一个选择是使用一个可能会更容易的HashMap。至少为了获取和设置与某人相关的选票。例如:Map candidate = new HashMap (); – 2011-04-21 15:30:01

+2

我完全同意。 Tom:一个迹象表明你的代码没有充分利用Java的面向对象的特性,这就是“静态”方法的泛滥。像这样的简单问题应该不需要静态方法 – 2011-04-21 15:30:53

0

通常情况下,您会使用下面的对象,因为Java是面向对象的语言。

class Votes { 
    final List<NameVote> nameVotes = new ArrayList<NameVote>(); 

    public void add(String name, int votes) { 
     nameVotes.add(new NameVote(name, votes)); 
    } 

    public void sort() { 
     Collections.sort(nameVotes, new Comparator<NameVote>() { 
      @Override 
      public int compare(NameVote o1, NameVote o2) { 
       return o1.votes - o2.votes; 
      } 
     }); 
    } 

    public String toString() { 
     StringBuilder sb = new StringBuilder(); 
     for (NameVote nameVote : nameVotes) { 
      sb.append(nameVote).append('\n'); 
     } 
     return sb.toString(); 
    } 
} 

class NameVote { 
    final String name; 
    final int votes; 

    NameVote(String name, int votes) { 
     this.name = name; 
     this.votes = votes; 
    } 

    @Override 
    public String toString() { 
     return "name='" + name + '\'' + 
       ", votes=" + votes; 
    } 
} 
相关问题