这里为什么写k==10就可以了#include
using namespace std;
int main()
{ int k, x;
int array[ ] = { 2, 3, 1, 7, 5, 8, 9, 0, 4, 6};
cout << "请输入要查找的元素值:";
cin >> x;
for (k = 0; k < 10; ++k)
if (x == array[k])
{ cout << "Find k, at position " << k; break;}
if (k == 10) cout << "not found";
return 0;
}
你的数组长度是10,但是,数组下标是从0开始的,当你遍历完你的数组发现没有找到,也就是说你那个时候数组下标为9,还不是结果,k值已经是通过++k变成了10,然后结束了循环了