如何在数组中找到最大数字

时间:2016-08-11 06:01:45

标签: c#

我有一个像这样的数组

int[] intnumber = new int[]{10,25,12,36,100,54,68,75,63,24,1,6,9,5};

我想找到最大的数字并按顺序从最大到最小

像这样 100,75,68,63,54,36,25,24,12,10,9,6,5,1

7 个答案:

答案 0 :(得分:1)

int[] intnumber = new int[] { 10, 25, 12, 36, 100, 54, 68, 75, 63, 24, 1, 6, 9, 5 };
int maxValue = intnumber.Max();

您可以按升序对数组进行排序以查看元素

Array.Sort(intnumber);
Array.Reverse(intnumber);
foreach (var str in intnumber )
{
    MessageBox.Show(str.ToString());
}

答案 1 :(得分:0)

 exactly output that you want.

   int[] intnumber = new int[] { 10,25,12,36,100,54,68,75,63,24,1,6,9,5 };
            Array.Sort<int>(intnumber ,
                            new Comparison<int>(
                                    (i1, i2) => i2.CompareTo(i1)
                            ));
                        intnumber .Dump();

P.S。要运行此演示,您需要执行以下步骤:

1.下载LINQPad

2.下载demo 文件,用LINQPad打开它,然后点击F5。

答案 2 :(得分:0)

您可以使用:

int[] intnumber = new int[]{10,25,12,36,100,54,68,75,63,24,1,6,9,5};

Array.Sort(intnumber );

Array.Reverse(intnumber );

int max = intnumber[0];

答案 3 :(得分:0)

试试这个,

        int[] intnumber = new int[] { 10, 25, 12, 36, 100, 54, 68, 75, 63, 24, 1, 6, 9, 5 };
        //Maximum Value
        int maxValue = intnumber.Max();
        //Maximum Index
        int maxIndex = intnumber.ToList().IndexOf(maxValue);

答案 4 :(得分:0)

我找到了你的帮助

            Console.WriteLine("How many Numbers Do you want? ");
        int counter = int.Parse(Console.ReadLine());
        double[] numbers = new double[counter];
        for (int i = 0; i < numbers.Length; i++)
        {
            Console.Write((i + 1) + " : ");
            numbers[i] = Convert.ToDouble(Console.ReadLine());
        }
        Console.WriteLine("_______________________________________________");
        Array.Sort(numbers);
        Array.Reverse(numbers);
        foreach (double item in numbers)
        {
            Console.WriteLine(item);
        }
        Console.WriteLine("_______________________________________________");
        Console.WriteLine("The Greatest Number is " + numbers[0]);
        Console.ReadKey();

答案 5 :(得分:0)

intNumbers成为您正在使用的数组,然后您可以使用Array类的.Max()方法获取最大值,即最大数。如果要对当前数组进行排序,则意味着必须使用.Sort()方法。要求只是按降序打印数组意味着您必须使用.OrderBy()

int[] inputNumbers = new int[] { 15, 12, 11, 23, 45, 21, 2, 6, 85, 1 };
Console.WriteLine("Input Array is              : {0}\n",String.Join(",",inputNumbers.OrderByDescending(x=>x)));
Console.WriteLine("Max value in the array is   : {0}\n",inputNumbers.Max());
Console.WriteLine("Array in descending order   : {0}\n",String.Join(",",inputNumbers.OrderByDescending(x=>x)));

这是working Example

答案 6 :(得分:-1)

int max = Integer.MIN_VALUE;

for (int i =0;  i < intnumber.length; i++)
{    
   int num = intnumber[i];

   //Check to see if num > max.   If yes, then max = num.   
}

System.out.println(max);