【问题标题】:sorting and printing scores from high to low [closed]从高到低排序和打印分数[关闭]
【发布时间】:2016-04-10 02:37:55
【问题描述】:

我需要为保龄球游戏编写代码,该代码将从用户那里获取姓名和分数,并在用户点击输入时停止而不写名称或分数。然后获取该信息并获取并打印分数的平均值,并从最高到最低打印分数和名称。这是我到目前为止所拥有的,但如果用户没有一直填写数组(在这种情况下最多为 10),我无法弄清楚排序代码以及如何在没有一堆 0 的情况下打印。

这是一个不同的类来获得平均分、最高分和最低分:

class BowlingScore
{

    public int LowScore(int[] scores, int j)
    {
        int min = scores.Where((v, i) => i < j).Min();
        return Array.IndexOf(scores, min);
    }

    public int HighScore(int[] scores)
    {
        int max = scores.Max();
        return Array.IndexOf(scores, max);
    }

    public double AverageScore(int[] numbers, int j)
    {
        double sum = 0;
        for (int i = 0; i < j; i++)
        {
            sum += numbers[i];
        }
        return (double)sum / j;
    }
    public void Swap(ref int a, ref int b)
    {

        int temp = a;
        a = b;
        b = temp;
    }

}

}

        and this is the main: static void Main(string[] args)
    {
        BowlingScore bs = new BowlingScore();
        const int MAX = 300;
        const int SIZE = 10;
        int i;

        // create an array with 10 elements
        string[] scoreInfo = new string[SIZE];
        string[] names = new string[SIZE];
        int[] scores = new int[SIZE];



        Console.WriteLine("Saturday Coder's Bpwling Team");
        Console.WriteLine("Enter in  a name and a score for each person on the team,");
        Console.WriteLine("For example, ''Mary 143''. Just hit Enter when you are done");

        for (i = 0; i < SIZE; i++)
        {
            Console.Write("Enter in a name and a score:  ");


            // Read one line of data from the file and save it in inputStr
            string inputStr = Console.ReadLine();
            // if statement to break when the user enters a zero
            if (inputStr == String.Empty)
            {
                break;
            }
            // The Split method creates an array of two strings
            scoreInfo = inputStr.Split();
            // Parse each element of the array into the correct data type
            names[i] = scoreInfo[0];
            scores[i] = int.Parse(scoreInfo[1]);
        }


        Console.WriteLine("The avarage score for this game was {0:N}.", bs.AverageScore(scores, i));
        int temp = 0;

        for ( i = 0; i < scores.Length; i++)
        {
            for (int j = 0; j < scores.Length - 1; j++)
            {
                if (scores[j] > scores[j + 1])
                {
                    temp = scores[j + 1];
                    scores[j + 1] = scores[j];
                    scores[j] = temp;
                }
            }
        }

        for (i = 0; i < scores.Length; i++)
            Console.Write($"{scores[i]}\n");




    // sort the array in ascending order
    // print out lots of messages so we can see the sort work

    Console.WriteLine();
        Console.ReadKey(true);


        Console.ReadLine();
    }
}

}

【问题讨论】:

  • 保留输入值数量的计数器。将其用于打印输出循环限制而不是 score.length。
  • 如何保持计数器?

标签: c# arrays sorting


【解决方案1】:
        List<int> scores = new List<int>();
        scores.Add(int.Parse(-your-string-input-)); //Add value to List
        scores.Min(); //Min Value
        scores.Max(); //Max Value
        scores.Average(); //Average
        scores.Sort(); //Sort the score List
        scores.Reverse(); //Reverse if necessary
        scores.Clear(); //Clear the score list

C# 用所有这些实用程序真的宠坏了你,但我建议你去学习一些 sorting algorithm,它对你作为程序员有好处

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-29
    • 1970-01-01
    • 2017-09-20
    • 2020-08-10
    • 1970-01-01
    • 2012-04-05
    相关资源
    最近更新 更多