这个问题为什么可以编译但是不
循环结束条件有误
正确代码应为:
#include <iostream>
using namespace std;
int main()
{
int array[] = {9, 12, 18, 22, 46, 9, 85, 78};
cout << "排序前的结果为: " << endl;
for (int i = 0; i < 8; i++)
{
cout << array[i] << " ";
}
cout << endl;
for (int i = 0; i < 8 - 1; i++)
{
for (int j = 0; j < 8 - 1 - i; j++)
{
if (array[j] > array[j + 1])
{
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
cout << "排序后的结果为: " << endl;
for (int i = 0; i < 8; i++)
{
cout << array[i] << " ";
}
cout << endl;
return 0;
}
应该是
for(int i=0;i<8-1;i++)
for(int j=0;j<8-1-i;j++)
你怎么都写成等于了呢
你基本的语法不行,建议从头到尾好好看===
你这怎么可能不显示 排序后?你中间的for 循环压根没有进去
注意你中间 for 循环的条件应该是 < 而不是==,当i 为0 的时候就进不去的