C# 在不使用Linq的情况下查找数组的最小最大值?

C# 在不使用Linq的情况下查找数组的最小最大值?,c#,arrays,linq,max,min,C#,Arrays,Linq,Max,Min,我为我的编程课做这项作业已经有一段时间了,尽管我已经把它解决了。给感兴趣的人一点信息:我的任务是从一系列分数(例如,int[]分数=4,5,6,7,8,9)中找到竞争对手的分数。但是当把其余的分数相加时,我不能包括最高和最低的值(例如,分数=5+6+7+8(不包括4和9)) 当我们被允许使用linq时,我的解决方案如下: using System; using System.Collections.Generic; using System.Linq; using System.Text; us

我为我的编程课做这项作业已经有一段时间了,尽管我已经把它解决了。给感兴趣的人一点信息:我的任务是从一系列分数(例如,int[]分数=4,5,6,7,8,9)中找到竞争对手的分数。但是当把其余的分数相加时,我不能包括最高和最低的值(例如,分数=5+6+7+8(不包括4和9))

当我们被允许使用linq时,我的解决方案如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Scoring {
class Program {


    static void Main(string[] args) {
        int[] scores = { 4, 7, 9, 3, 8, 6 };

        remove_duplicates(scores);
        console_text(scores);


        ExitProgram();
    }


    static void remove_duplicates(int[] scores) { //removes duplicat values from array

        var pScores = scores.Distinct().ToArray();

    }



    static int result(int[] pScores) { //calculates results by removing max and min scores, then adding the remaining.

        return pScores.Sum() - pScores.Max() - pScores.Min();

    }


    static void console_text(int[] pScores) { //renders the text in the consol window

        int final_score = result(pScores);


        Console.Write("Competitor your scores were " );
        foreach (int i in pScores) {
            Console.Write(" " + i);
        }
        Console.WriteLine("\r\n" + "           and your score was " + final_score);

    }





    static void ExitProgram() {
        Console.Write("\n\nPress any key to exit program: ");
        Console.ReadKey();
    }//end ExitProgram

}
}
因此,我认为我已经完成了,但现在我收到了一封电子邮件,声明:

“不能使用任何system.array方法,也不能使用Linq的任何功能或可能发现的任何其他库类”

这让我有点不知所措,任何帮助都将不胜感激


谢谢

这里有一个选择。唯一不太清楚的是天气,如果有两个相等的值也是最小值或最大值,则应同时删除或仅删除一个最大值和一个最小值。我只删除一个最小值和一个最大值:

int[] arr = new int[] { 2, 2, 3, 4, 5, 6, 6 };

        int Lowest = arr[0];
        int Highest = arr[0];
        int sum = 0;
        for (int i = 0; i < arr.Length; i++)
        {
            if (Lowest > arr[i])
            {
                Lowest = arr[i];
            }
            if (Highest < arr[i])
            {
                Highest = arr[i];
            }
            sum += arr[i];
        }
        sum -= (Lowest + Highest);
        Console.WriteLine("The sum withot the highest and lowest score is : {0}", sum);

这样,您将对所有其他值求和,无论它们在数组中出现多少次。只有一个
MAX
和一个
MIN
值添加到总和。

并且没有重复值?一种方法是使用临时变量在数组中循环,将下一个值与当前值进行比较-如果小于当前值,则为新的最小值(或者如果大于当前值,则为新的最大值)。在循环结束时,您将获得最小(或最大)值。似乎您必须按旧方法执行。试试提姆的建议。使用for语句获取arrray中的最小值和最大值。然后求和,不包括最小值和最大值。或者您可以先对它们进行排序,然后在排序后从索引1求和到score.length-2。你的选择。这比我描述的要复杂一点。我建议在谷歌上搜索在集合(或数组)中查找最小/最大/不同值的算法。感谢各位提供的提示,只是想知道,排序方法不是来自system*的吗?谢谢回复!真的很感激!如果不太麻烦的话,我只是想澄清一些事情。由于我不熟悉编程和c#,请你解释一下这句话:“inti=0;Iloop。为了能够处理数组的每个元素,我必须首先提取它。由于
array
对每个元素使用唯一的索引,并且该索引是基于零(0)的,因此我创建了一个循环以从数组中获取每个元素。因为每个数组都是基于零的,所以我有
for(int I=0..
所以以后我可以得到我的零元素
arr[I]
,它被翻译成
arr[0]
当这是我们第一次进行循环时。之后,
i
是1,2,3,所以我得到下一个元素。这就是本例中
for循环的目的。@ps2goat这就像一堂历史课,我认为很好的一点是,在学习的同时,他们从一开始就开始了。很高兴知道从哪里开始所以你知道为什么事情会发展到现在。我同意。最好的开发人员不仅知道如何,而且知道为什么事情会这样做。
int[] arrOfHighest = new int[arr.Length];
        int[] arrOfLowest = new int[arr.Length];
        int minCounter = 0;
        int maxCounter = 0;
        for (int i = 0; i < arr.Length; i++)
        {
            if (Lowest == arr[i])
            {
                arrOfLowest[minCounter] = arr[i];
                minCounter++;
            }
            if (Highest == arr[i])
            {
                arrOfHighest[maxCounter] = arr[i];
                maxCounter++;
            }
        }
        int sumLoewst = 0;
        for (int i = 0; i < arrOfLowest.Length; i++)
        {
            sumLoewst += arrOfLowest[i];
        }
        int sumHighest = 0;
        for (int i = 0; i < arrOfHighest.Length; i++)
        {
            sumHighest += arrOfHighest[i];
        }
        sum -= (sumLoewst + sumHighest);
sum -= (sumLoewst + sumHighest); 
sum += (Highest + Lowest);