大师们 ,冒泡打印,看看哪错了呢

来源:百度知道 编辑:UC知道 时间:2024/05/14 05:29:36
static void Main(string[] args)
{
int temp = 0;
int[] num = { 2, 1, 3, 4, 5 };
for (int i = 0; i < num.Length-1; i++) {
for (int j = 0; j<num.Length-1-i; j++) {

if (num[j] < num[j+1]) {
temp=num[j];
num[j] = num[j+1];
num[j+1] = temp;

}
}
Console.WriteLine(num[i]);

}
}

在排序后,最后还要重新打印一个For循环才行,就在你最后的那个Console.WriteLine(num[i]);
改成
for(int i = 0;i <num.length;i++)
{
Console.writeline(num[i]);
}

Console.WriteLine(num[i]);中使用的i是上面for定义的局部变量,这一句写在for外边,当然用不了i了