第1个回答 2014-03-17
int[] a ={ 85, 23, 16, 8, 49 };
for (int i = 0; i < 2; i++) //只用循环2次
{
for (int j = i + 1; j < a.Length; j++)
{
if (a[i] < a[j]) //把最大值放在索引i位置
{
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
Console.WriteLine("最大值:{0}", a[0]);
Console.WriteLine("次大值:{0}", a[1]);
第2个回答 2014-03-17
int[] numbers = { 1, 2, 5, 4, 3 };
int i, j;
int temp;
for (i = 0; i < numbers.Length-1; i++)
{
for (j = 0; j < numbers.Length-1; j++)
{
if (numbers[j]>numbers[j+1])
{
temp = numbers[j];
numbers[j] = numbers[j + 1];
numbers[j + 1] = temp;
}
}
}
Console.WriteLine("最大值是:"+numbers[4]);
Console.WriteLine("次大值是:"+numbers[3]);
望采纳~本回答被网友采纳