【问题标题】:C++ Repeating an inquiry with a user's inputC ++使用用户输入重复查询
【发布时间】:2014-06-26 08:06:39
【问题描述】:

我想要做的是问一个简单的问题,使用 1 或 2 作为输入的是或否。如果他们说 1 或 2,那么它将继续使用代码。但是如果他们说数字(7)或字符('q'),那么问题将再次重复。我还没有弄清楚如何重复问题和输入。 这是我目前所拥有的:

#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
int answer = 0;

do
{
cout << "Are you Bald? Yes(1) or No(2)?" << endl;
cin >> answer; cout << endl;

    if (answer == 1)
    {
        cout << "You Are Bald." << endl;
    }

    else  if (answer == 2)
    {
        cout << "You are not bald." << endl;
    }

    else { cout << "Please Input proper answer" << endl << endl; 
} while (answer < 1, answer > 2);
system("pause");
return 0;

【问题讨论】:

    标签: visual-c++ if-statement input while-loop


    【解决方案1】:

    试试这个:

        #include "stdafx.h"
        #include <iostream>
        using namespace std;
    
        int main()
        {
        int answer = 0;
        while(1)
        {
            cout << "Are you Bald? Yes(1) or No(2)?" << endl;
            cin >> answer; cout << endl;
    
            if (answer == 1)
            {
                cout << "You Are Bald." << endl;
                break;
            }
    
            else  if (answer == 2)
            {
                cout << "You are not bald." << endl;
                break;
            }
    
            else 
            { 
               cout << "Please Input proper answer" << endl << endl; 
            }
    
        }
        return 0;
    }
    

    【讨论】:

    • 我试过这个,但即使输入了 1 或 2,它最终还是会重复问题。
    猜你喜欢
    • 2016-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-06
    • 1970-01-01
    • 2017-12-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多