简体   繁体   English

如何显示数组的最高和最低

[英]How to display highest and lowest of an array

here i ask the user for homework scores which are then averaged after discarding the smallest and largest score. 在这里,我向用户询问作业分数,然后在丢弃最小和最大分数后将其平均。 i have stored the user input in an array. 我已经将用户输入存储在数组中。 in my DisplayResults method im not sure how to display the lowest and highest scores that were discarded. 在我的DisplayResults方法中,im不确定如何显示被丢弃的最低和最高分数。 any help is appreciated! 任何帮助表示赞赏! Here is what i have so far: 这是我到目前为止所拥有的:

class Scores
{
    static void Main(string[] args)
    {
        double sum = 0;
        double average = 0;
        int arraySize;
        double[] inputValues;

        arraySize = HowManyScores();

        inputValues = new double[arraySize];
        GetScores(inputValues);
        sum = CalculateSum(inputValues);
        average = CaculateAverage(sum, arraySize);

        DisplayResults(inputValues, average);
        Console.Read();
    }

    public static int HowManyScores()
    {
        string input;
        int size;
        Console.WriteLine("How many homework scores would you like to enter?");
        input = Console.ReadLine();
        while (int.TryParse(input, out size) == false)
        {
            Console.WriteLine("Invalid data. Please enter a numeric value.");
            input = Console.ReadLine();
        }

        return size;
    }

    public static void GetScores(double[] inputValues)
    {
        double scoreInput;
        Console.Clear();

        for (int i = 0; i < inputValues.Length; i++)
        {
            scoreInput = PromptForScore(i + 1);
            inputValues[i] = scoreInput;
        }
    }

    public static double PromptForScore(int j)
    {
        string input;
        double scoreInput;

        Console.WriteLine("Enter homework score #{0}:", j);
        input = Console.ReadLine();

        while (double.TryParse(input, out scoreInput) == false)
        {
            Console.WriteLine("Invalid Data. Your homework score must be a numerical value.");
            input = Console.ReadLine();
        }

        while (scoreInput < 0)
        {
            Console.WriteLine("Invalid Data. Your homework score must be between 0 and 10.");
            input = Console.ReadLine();
        }

        while (scoreInput > 10)
        {
            Console.WriteLine("Invalid Data. Your homework score must be between 0 and 10.");
            input = Console.ReadLine();
        }

        return scoreInput;
    }

    public static double CalculateSum(double[] inputValues)
    {
        double sum = 0;
        for (int i = 1; i < inputValues.Length - 1; i++)
        {
            sum += inputValues[i];
        }

        return sum;
    }

    public static double CaculateAverage(double sum, int size)
    {
        double average;
        average = sum / ((double)size - 2);
        return average;
    }

    public static void DisplayResults(double[] inputValues, double average)
    {
        Console.Clear();
        Console.WriteLine("Average homework score: {0}", average);

        //Console.WriteLine("Lowest score that was discarded: {0}",
        //Console.WriteLine("Highest score that was discarded: {0}",
    }
}

} }

You basically only have to do one thing: Sorting the array after you received your input data. 基本上,您只需要做一件事:在收到输入数据后对数组进行排序。 Then, printing the first and last value gives you the minimal and maximal score. 然后,打印第一个和最后一个值将为您提供最小和最大分数。 Use 采用

Array.Sort(intArray);

in main after calling GetScores and main调用后GetScores

 Console.WriteLine("Lowest score: {0} Highest score: {1}", 
    inputValues[0], inputValues[inputValues.Length - 1]);

to print the results. 打印结果。 Cheers 干杯

EDIT: The proposal by Jens from the comments using the Min/Max is probably more what you're looking for if you're not interested in complete ordering of your values. 编辑:如果您对值的完整排序不感兴趣,则詹斯的建议使用最小/最大注释可能会更多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在给定数组中找到最高,第二高的数字,最低的第二低的数字 - How to find highest ,second highest number, Lowest Second Lowest number in given Array 如何对拆分数组进行排序以从高到低读取? - How do I sort a split array to read highest to lowest? 显示从最低到最高的数字系列 - Display series of numbers from lowest to highest 如何找到数组的最小值和最大值以及数组的平方和反转的数组? - How do I find the lowest and highest value of an array as well as the array squared and the array reversed? 如何使用内置函数显示列表中重复次数最少和最多的数字? - How do you display the number that has the lowest and highest amount of duplicates in a list w/o using built in functions? 如何从最高到最低排序一个数组,并允许用户输入数组的一部分,而不是全部输入? - how to sort an array from highest to lowest and allowing the user to enter in part of the array, not the full amount alotted? 如何从列表中找到最低和最高时间? - How to find lowest and highest time from list? 如何使用swap方法将数组从最高到最低排序? - How do I sort the array from highest to lowest using the swap method? 如何找到最高和最低的数字C# - How to find the highest and the lowest number C# 如何从范围中获得最低和最高数字 - How to get lowest and highest number from range
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM