using System;
using System.Collections.Generic;
using System.Text;namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{ int[] aa = new int[] { 1,2,3,4,5};
int i=aa.Length;
int temp = 0;
for (int m = 0; m< aa.Length-1; i++) for (int j = m + 1; j < aa.Length;j++ )
{
if (aa[i] <aa[j])
{
temp = aa[i];
aa[i] = aa[j]; } }
}
}
}运行时 说索引超出了数组界限
这个错误是为何?
怎么改?
using System.Collections.Generic;
using System.Text;namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{ int[] aa = new int[] { 1,2,3,4,5};
int i=aa.Length;
int temp = 0;
for (int m = 0; m< aa.Length-1; i++) for (int j = m + 1; j < aa.Length;j++ )
{
if (aa[i] <aa[j])
{
temp = aa[i];
aa[i] = aa[j]; } }
}
}
}运行时 说索引超出了数组界限
这个错误是为何?
怎么改?
{
if (aa[i] <aa[j])
{
temp = aa[i];
aa[i] = aa[j]; }
}
} 改成
for (int m = 0; m < aa.Length; i++)
{
for (int j = m; j < aa.Length-1;j++ )
{
if (aa[i] <aa[j])
{
temp = aa[i];
aa[i] = aa[j]; }
}
}