如何对拆分数组进行排序以读取从高到低

本文关键字:读取 排序 拆分 数组 | 更新日期: 2023-09-27 18:33:31

我需要一点帮助,将拆分数组从高到低排序,同时将名称保留在分数旁边。我有点不确定该怎么做,因为数组是分开的。另外,有没有办法让用户输入任意数量的名称和分数,而不会让程序给用户带来错误?因此,如果他们只想输入 4 个姓名和分数,他们所要做的就是按回车键?

这是我到目前为止的代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace proj09LEA
{
    class Program
    {
        static void Main(string[] args)
        {
            // declare and array of integers          
            string[] name = new string[5];
            int[] score = new int[5];
            Console.WriteLine("'nSaturday Coder's Bowling Team");
            Console.WriteLine("Enter in a name and score for each person on the team.");
            Console.WriteLine("For example, Mary 143. Just hit Enter when you are done.'n");
            // fill an array with user input
            for (int i = 0; i < score.Length; i++)
            {
                Console.WriteLine("Enter in a name and score: ");
                string line = Console.ReadLine();
                name[i] = line.Substring(0, line.IndexOf(' '));
                score[i] = int.Parse(line.Substring(line.IndexOf(' ') + 1));
            }
            Console.WriteLine("------------ Input Complete ------------'n");
            Console.WriteLine("Here are the scores for this game, from highest to lowest:'n");
            for (int i = 0; i < score.Length; i++)
            {
                if (score[i] >= 300)
                {
                    Console.WriteLine("{0}'s score was {1}*.", name[i], score[i]);
                }
                else
                {
                    Console.WriteLine("{0}'s score was {1}.", name[i], score[i]);
                }
            }
            AverageScore(score);
            Console.WriteLine("Press Enter to continue. . .");
            Console.ReadLine();
        }
        static void AverageScore(int[] score)
        {
            int sum = score.Sum();
            int average = sum / score.Length;
            Console.WriteLine("The average score for this game was {0:d}.'n", average);
        }
    }
}

如何对拆分数组进行排序以读取从高到低

让我先谈谈无限玩家的问题。如您所知,数组的大小在创建时是固定的。不过,有一种数据结构List可以具有无限(嗯,实际上(数量的元素。您可以创建一个这样的:

List<string> names = new List<string>();

然后,如果要添加新名称,则可以使用

names.Add("Mary");

代码的其余部分应该大致相同;索引正常工作,求和正常工作,等等。


现在将它们两个排序在一起怎么样?好吧,你真的没有名字列表和分数列表;你真正在语义上拥有的是一对名字和分数的列表,或者一个球员列表。您可以先定义一个表示玩家的结构:

struct Player {
    public string Name { get; set; }
    public int Score { get; set; }
    public Player(string name, int score) {
        Name = name;
        Score = score;
    }
}

然后你可以有一个玩家列表:

List<Player> players = new List<Player>();

我们不是单独添加姓名和分数,而是将它们加在一起:

string name = /* ... */;
int score = /* ... */;
players.Add(new Player(name, score));

这样表示,您的打印例程也变得更加简单。您可以同时循环访问两者:

foreach(Player player in players) {
    Console.WriteLine("{0} scored {1}", player.Name, player.Score);
}

最后,求和有点棘手,但不会太难。基本上,我们提取所有分数并汇总这些分数:

int sum = players.Select((player) => player.Score).Sum();

但对你来说,真正的好处是终于能够对它进行排序:

players.Sort((x, y) => y.Score - x.Score);

你基本上需要的是看看HashMaps,它是ADT专门用于此目的的。其结构如下:
-键 1 : 值 1
-键 2 : 值 2
.....
-Keyn : Valuen
你在这里的关键应该是你的分数(但它可以是任何东西(。该值将是与分数关联的名称。
然后,您可以创建一个方法sortKeys,具体取决于您的实现,该方法将获取HashMap中的每个键,将其移动到末尾或开头,同时指向您的名称。
但请注意,一些 HashMap 是随机存储的,因此您可能必须创建一个数组来写入键的排序顺序(然后在 HashMap 中查找键的值(。

首先使用 Arrays.sort 方法对数组进行排序,然后从

 for(int i= x.length-1;i>=0;i--)
     System.out.print(x[i]+"");

应该这样工作

列表将是一个更好的选择,这些选项带有内置的排序选项,可以使您的任务更轻松。