在随机数组中查找最大数字及其索引

时间:2014-10-16 18:46:11

标签: loops random

这是我目前生成一组20个随机数的代码:

  int[] array_1 = new int[20];
        Random rand = new Random();

        for (int i = 0; i < array_1.Length; i++)
        {
            array_1[i] = rand.Next(10,100);
            Console.Write("{0} ", array_1[i]);



        }

1 个答案:

答案 0 :(得分:1)

int highest = 0;
int highestIndex = 0;

for (int i = 0; i < array_1.Length; i++) 
{
    if (array_1[i] > highest) 
    {
        highest = array_1[i];
        highestIndex = i;
    }
}