Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 排序算法[实现为一种方法]_Java_Algorithm_Sorting - Fatal编程技术网

Java 排序算法[实现为一种方法]

Java 排序算法[实现为一种方法],java,algorithm,sorting,Java,Algorithm,Sorting,我想了解一下如何使用排序算法问题方法中代码的最后一个方法,以便按顺序对每个玩家的名字进行排序。我不能将它放在Main方法中,因为它需要来自Questions方法中的循环的名称字符串 public static void main(String[] args){ int players = Integer.parseInt(JOptionPane.showInputDialog("Please tell me how many players are taking part in

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

    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

}

让您的提问方法返回带有玩家姓名的数组。
这样,您就可以使用返回的值调用sortNames方法。

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

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

如果你想让游戏变得简单,那么你可以为每个玩家创建一个对象,并将其保存在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);

我应该如何在提问方法中实现这一点?另外,我使用了这个复杂的方法,因为这是我们经历过的一个算法。我想使用我已经拥有的算法,因为我不想把代码搞得一团糟。有没有办法将sortName方法传递到playerscore显示上方的questions方法中,以便按顺序对名称进行排序?没有,你不能这样做,因为如果你对名称进行排序,那么另一张地图中的球员的相应分数将指向旧名称顺序。因此,对于两个不同的数组,不能单独对一个数组进行排序。当我尝试从question方法返回name数组时,它表示该值已经声明。
Map<String, Object> map = new HashMap<String, Object>();
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);