第一种方法:
int[] arr = new int[10];
for (int i = 0; i < arr.Length; i++)
{
Console.WriteLine("请输入一个数");
arr[i] = int.Parse(Console.ReadLine());
}
int max = -100;
int index = -1;
for (int i = 0; i < arr.Length; i++)
{
if (max < arr[i])
{
max = arr[i];
index = i;
}
}
Console.WriteLine("最大值是{0},下标是{1}", max, index);
第二种方法:
int[] arrs = { 7, 8, 5, 12, 36, 2, 9, 1, 3, 4 };
int max = 0;
int index = 0;
for (int i = 0; i < arrs.Length; i++)
{
if (arrs[i] > max)
{
index = i;
max = arrs[i];
}
}
Console.WriteLine("max={0},index={1}", max, index);