排序和打印分数从高到低

时间:2016-04-10 02:37:56

标签: c# arrays sorting

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

这是获得平均分,最高分和最低分的不同类:

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();
    }
}

}

1 个答案:

答案 0 :(得分: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,这对你作为程序员有好处