阿姆斯壮数C++
如何询问用户是否要继续 如果正确或错误: 输出如下所示: 请输入一个数字以检查是否为阿姆斯特朗号码:371 371 是阿姆斯特朗号码,是否继续?再见 如果写“Y”,它将在同一个问题中继续 我工作:阿姆斯壮数C++,c++,loops,C++,Loops,如何询问用户是否要继续 如果正确或错误: 输出如下所示: 请输入一个数字以检查是否为阿姆斯特朗号码:371 371 是阿姆斯特朗号码,是否继续?再见 如果写“Y”,它将在同一个问题中继续 我工作: while (y != 0) { d=y%10; z=z+(d*d*d); y=y/10; } if ( z == x ) { cout <<x <<" is an Armstrong Number" << endl; } if ( z
while (y != 0)
{
d=y%10;
z=z+(d*d*d);
y=y/10;
}
if ( z == x )
{
cout <<x <<" is an Armstrong Number" << endl;
}
if ( z != x )
{
cout <<x << " is not an Armstrong Number"<<endl;
}
cout << "Do you want to continue (Y/N) ? " ;
cin >> m;
do{
cout <<"Please enter a number to check if it is an Armstrong number:";
cin >> x;
if ( z == x )
{
cout <<x <<" is an Armstrong Number" << endl;
}
if ( z != x )
{
cout <<x << " is not an Armstrong Number"<<endl;
}
cout << "Do you want to continue (Y/N) ? " ;
cin >> m;
}while ( m == 'Y' || m == 'y') ;
cout << "Bye"<<endl;
但这是错误的 在声明变量后,启动do while循环:do{;其中“ans”是存储用户答案的char变量。如下所示:
//Here are the declared variables.
do{
cout << "Please enter a number to check if it is an Armstrong number: ";
cin >> number; //Replace 'number' with the variable name you have used to store that number.
//The algorithm, which tests if the number is Armstrong number or not, goes here.
cout << "Do you want to continue (Y/N)? ";
cin >> ans;
}while(ans == 'Y');
cout << "Bye";
您可以将整个内容封装在另一个while循环中,切换后,如果输入为n,则中断;我从来没有见过一个开关/箱子被塞进一个应该比这个更糟糕的if/else中你的意思是:如果z==x cout@Lablabla,我做了,但它运行不正确!来自《惠利!》0'直到'cout'
while (y != 0)
{
d=y%10;
z=z+(d*d*d);
y=y/10;
}
if ( z == x )
{
cout <<x <<" is an Armstrong Number" << endl;
}
if ( z != x )
{
cout <<x << " is not an Armstrong Number"<<endl;
}
cout << "Do you want to continue (Y/N) ? " ;
cin >> m;
do{
cout <<"Please enter a number to check if it is an Armstrong number:";
cin >> x;
if ( z == x )
{
cout <<x <<" is an Armstrong Number" << endl;
}
if ( z != x )
{
cout <<x << " is not an Armstrong Number"<<endl;
}
cout << "Do you want to continue (Y/N) ? " ;
cin >> m;
}while ( m == 'Y' || m == 'y') ;
cout << "Bye"<<endl;
//Here are the declared variables.
do{
cout << "Please enter a number to check if it is an Armstrong number: ";
cin >> number; //Replace 'number' with the variable name you have used to store that number.
//The algorithm, which tests if the number is Armstrong number or not, goes here.
cout << "Do you want to continue (Y/N)? ";
cin >> ans;
}while(ans == 'Y');
cout << "Bye";