C++ 从数组列表中,用户搜索一个数字(如果未找到),然后仅显示一个未找到的消息(未多次) #包括 #包括 使用名称空间std; int main() { int-usman[10]={0,5,6,3,5,1,8,5,4}; 智力选择; coutchoice; cout
添加一个额外的布尔变量以检查是否找到该数字。如果多次找到该数字,则多次应用C++ 从数组列表中,用户搜索一个数字(如果未找到),然后仅显示一个未找到的消息(未多次) #包括 #包括 使用名称空间std; int main() { int-usman[10]={0,5,6,3,5,1,8,5,4}; 智力选择; coutchoice; cout,c++,arrays,loops,C++,Arrays,Loops,添加一个额外的布尔变量以检查是否找到该数字。如果多次找到该数字,则多次应用found=true与仅应用一次相同 #include<iostream> #include<conio.h> using namespace std; int main() { int usman[10]={0,5,6,3,5,1,8,5,4}; int choice; cout<<"Please Neter A Value To search : "; cin&
found=true
与仅应用一次相同
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int usman[10]={0,5,6,3,5,1,8,5,4};
int choice;
cout<<"Please Neter A Value To search : ";
cin>>choice;
cout<<endl;
for( int j=0 ; j<10 ; j++)
{
if(usman[j]==choice)
{
cout<<"usman["<<j<<"] = "<<choice<<endl;
}
}
getch();
return 0;
}
boolfound=false;
对于(int j=0;j您可以这样做:-
把这个放在开头:
bool found = false;
for( int j=0 ; j<10 ; j++)
{
if(usman[j]==choice)
{
cout<<"usman["<<j<<"] = "<<choice<<endl;
found = true;
}
}
if (!found)
{
cout << "The given number does not exist!" << endl;
}
之后,将check=1;
放在if
的末尾
然后在for
循环之后,放入以下内容:
int check=0;
if(i==10&&check!=1)
{
库特
if (i == 10 && check != 1)
{
cout<<"The entered value is not found";
}