排序算法[实现方法]

时间:2014-12-08 13:21:20

标签: java algorithm sorting

我想了解如何在Questions方法中使用排序算法(代码的最后一种方法),以便按顺序对每个玩家的名称进行排序。我不能将它放在Main方法中,因为它需要来自Question Method中循环的(名称)字符串。

    public static void main(String[] args){
    int players = Integer.parseInt(JOptionPane.showInputDialog("Please tell me how many players are taking part in this quiz!"));


            /* --------------------------- Questions in Arrays --------------------------- */ 

        String[] question = {"Question 1: ",
                         "Question 2: ",
                         "Question 3: ",
                         "Question 4: ",
                         "Question 5: " };

            String[] answer = {"ans1",
                               "ans2",
                               "ans3",
                               "ans4",
                               "ans5" };




questions(question, answer, players);

            /* --------------------------- Questions in Arrays --------------------------- */ 
}                       /* End of Main Method */




            /* Questions Method */

public static void questions(String[] question, String[] answer, int n) {

        String[] name = new String[n];  // Player Names 
        int[] playerscore = new int[n]; // Argument for Score
        String[] que = new String[question.length]; //Questions for Loops



            /* --------------------------- For loop for number of players --------------------------- */ 
    for (int i = 0; i < n; i++)
    {
        playerscore[i] = 0;
        name[i] = JOptionPane.showInputDialog("What is your name player"+ (i+1) +"?");
        JOptionPane.showMessageDialog(null,"Hello :"+ name[i] + " Player number " +(i+1)+ ". I hope your ready to start!");


            /* --------------------------- Loop in Loop for questions --------------------------- */ 
        for (int x=0; x<question.length; x++) {
            que[x] = JOptionPane.showInputDialog(question[x]);
                if(que[x].equals(answer[x]))
                    {

                        playerscore[i] = playerscore[i] + 1;

                    }

        else {
                JOptionPane.showMessageDialog(null,"Wrong!");
             }

                } // End for loop for Question



        System.out.println("\nPlayer: "+(i+1)+ " Name: "+name[i]+"\tScore"+playerscore[i]+" out of 5.);





            /* --------------------------- Loop in Loop for questions --------------------------- */ 



           } // End for loop for player number

              } //End method questions

public static void sortNames(String name []){

int i;
boolean flag = true;  // will determine when the sort is finished
String temp;

  while(flag) { // while flag is true
    flag = false;
        for (i = 0; i < name.length-1; i++) {
          if (name[i].compareToIgnoreCase(name[i+1]) > 0 ) {
            temp = name[i];
            name[i] = name[i+1];     // swapping
            name[i+1] = temp;
            flag = true;
                }
            }
    }

}

       /* --------------------------- Score Display --------------------------- */ 

}

2 个答案:

答案 0 :(得分:0)

让你的问题方法返回带有玩家名字的数组 这样您就可以使用返回的值来调用sortNames方法。

答案 1 :(得分:0)

为什么不使用Arrays.sort()方法对名称进行排序?您正在使用复杂的方法对字符串进行排序。

public static String[] sortNames(String name[]) 
{
    Arrays.sort(name);
    return name;
}

供参考Sort Array in Java

如果你想轻松,那么你可以为每个玩家创建一个对象并将其保存在HashMap中。

你可以在java中使用HashMap,使用key作为你的玩家名称 - String和Value作为一个对象,包含你需要拥有的玩家的所有信息。

Map<String, Object> map = new HashMap<String, Object>();

您也可以像

那样对该hashmap进行排序
HashMap<String, PlayerInfo> map = new HashMap<String, PlayerInfo>();
ValueComparator bvc =  new ValueComparator(map);
TreeMap<String,PlayerInfo> sorted_map = new TreeMap<String,PlayerInfo>(bvc);

map.put(playerName1, playerInfo1);
map.put(playerName2, playerInfo2);

map.put(playerNamen, playerInfon);

sorted_map.putAll(map);

System.out.println("results: "+sorted_map);