如何在 Java 中对文本文件进行冒泡排序

发布于 2024-09-27 01:53:41 字数 1089 浏览 5 评论 0原文

我如何在java中进行冒泡排序, 文本文件如下所示:

aaa

2

bbb

3

ccc

1

我需要做的是循环遍历它并将最高分数显示到最低分数。 当我运行下面的代码时,数字已经排序。它将显示 3、2,然后是 1。 但该名称与相应的乐谱不同步,它只会根据第一个显示名称。因此,如果我运行下面的代码,输出基本上如下所示:

aaa  3

bbb  2

ccc  1

函数:

public void hi_score(){
    int i=0;
        try {
                 FileReader fr;
          fr = new FileReader (new File("F:\\pscores.txt"));
          BufferedReader br = new BufferedReader (fr);
          int ar=0;
          for(ar=0;ar<10;ar++){
          player_name[ar]=br.readLine();
          player_score[ar]=Integer.parseInt(br.readLine());
          }

            } catch (Exception e) { e.printStackTrace();}

              bubble_srt(player_score, player_score.length);
        System.out.print("Highscores:\n");
        System.out.println("Scores");

        System.out.println("Name\tScore");
        for(i = 0; i <NUMBER_OF_HI_SCORE; i++){
          System.out.print(player_name[i] + "\t" +player_score[i] + "\n");

        }
    }

如何在不使用 arraylist 的情况下执行此操作。只是普通的数组。

How do I do a bubble sort in java,
The text file looks like this:

aaa

2

bbb

3

ccc

1

What I need to do is to loop through it and display the highest score to the lowest.
When I run the code below, The numbers, are already sorted. and it will display 3, 2, then 1.
But the name isn't in sync with the corresponding score, it will only display the names based on what's first. So the output will basically look like this, if I run the code below:

aaa  3

bbb  2

ccc  1

Function:

public void hi_score(){
    int i=0;
        try {
                 FileReader fr;
          fr = new FileReader (new File("F:\\pscores.txt"));
          BufferedReader br = new BufferedReader (fr);
          int ar=0;
          for(ar=0;ar<10;ar++){
          player_name[ar]=br.readLine();
          player_score[ar]=Integer.parseInt(br.readLine());
          }

            } catch (Exception e) { e.printStackTrace();}

              bubble_srt(player_score, player_score.length);
        System.out.print("Highscores:\n");
        System.out.println("Scores");

        System.out.println("Name\tScore");
        for(i = 0; i <NUMBER_OF_HI_SCORE; i++){
          System.out.print(player_name[i] + "\t" +player_score[i] + "\n");

        }
    }

How do I do this, without using arraylist. Just ordinary array.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

星星的軌跡 2024-10-04 01:53:41

应该创建一个将玩家和相应得分组合为 String[] 的数组;
创建一个进行冒泡排序的分数数组,

然后当您有一个排序的 int[]player_score 数组时,您可以尝试通过 String[]playername+scoreedit 中元素的子字符串来匹配它


因此,在冒泡排序之后,当您要打印它时,也许可以采用新排序的分数数组,尝试与 String[] playername + Score 数组中的适当元素进行匹配,然后打印出与第 0 个匹配的元素,排序分数数组的第一个、第二个值。

Should create a single array that combines the player and the respective score as an String[];
create a single array of scores that gets bubble sorted

then as you have an sorted int[] player_score array, you can try to match it up via substring of the elements in String[] playername + score

edit:
so after the bubble sort, when you are going to print it, perhaps take the newly sorted array of scores, try to match with an appropriate element of the array of String[] playername + score, then print out the element that matches the 0th, 1st, 2nd values of the sorted score array.

只是一片海 2024-10-04 01:53:41

这是因为您仅对 player_score 数组进行排序:

bubble_srt(player_score, player_score.length);

您还需要将 player_name 数组发送到函数,按照之前的排序方式(基于得分)进行排序,并且每个当你交换两个乐谱时,你也必须交换名字。

This is because you are sorting only the player_score array:

bubble_srt(player_score, player_score.length);

You also need to send the player_name array to the function, sort as you were sorting before(based on score) and every time you swap two scores, you'll have to swap the names as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文