阿姆斯特朗编号C ++

时间:2014-11-15 06:21:39

标签: c++ loops

如何继续询问用户?

while(y != 0)
{
    d=y%10;
    z=z+(d*d*d);
    y=y/10;
}


如果是真或假:

switch ( z == x )
{
case true :cout <<x <<" is an Armstrong Number" << endl;
break ;
case false :cout <<x << " is not an Armstrong Number"<<endl;
}

输出如下:

  

请输入一个号码以检查它是否为阿姆斯壮号码:371
371   是阿姆斯特朗号码你想继续吗(是/否)? N
再见!


如果写'Y&#39;它会继续在同一个问题上。

我工作:

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;

但是错了!

1 个答案:

答案 0 :(得分:0)

声明变量后,启动do-while循环:do{。在询问用户是否要继续或不关闭它的部分之后,如}while(ans == 'Y');其中'ans'是存储用户答案(Y / N)的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";