在 do...while 中放置一个开关

发布于 2024-11-10 16:49:55 字数 1353 浏览 1 评论 0原文

所以我有一个 do...while 语句,除非用户输入四件事之一,否则应该重复该语句。这四件事有一个中断,它们将控制 while 的变量设置为 true,默认值为 false。如果用户没有输入四个答案之一,则该变量将保持 false,这将使 do...while 重新开始。事实并非如此。

#include <string>
#include <iostream>

using namespace std;

int main()
{
    int q5_answer = 1;
    int q5_answerU;
    int total_score;
    bool q5_valid = false;

    //Question_5:
    do
    {
        cout << "Question 5 (#1 is correct)" <<endl;
        cout << "1.) Answer 1" <<endl;
        cout << "2.) Answer 2" <<endl;
        cout << "3.) Answer 3" <<endl;
        cout << "4.) Answer 4" <<endl;
        cin >> q5_answerU;

        switch (q5_answerU)
        {
        case 1:
            cout << "Correct!" <<endl;
            q5_valid = true;
            (total_score = total_score + 1);
            break;
        case 2:
            cout << "Incorrect" <<endl;
            q5_valid = true;
            break;
        case 3:
            cout << "Incorrect" <<endl;
            q5_valid = true;
            break;
        case 4:
            cout << "Incorrect" <<endl;
            q5_valid = true;
            break;
        default:
            cout << "Invalid answer" <<endl;
        }
    } while (q5_valid = false);
}

So I have a do... while statement that is supposed to repeat unless the user enters one of four things. Those four things have a break and they set the variable that controls the while to true, where the default is false. If the user does not enter one of the four answers, the variable will stay false, which should make the do...while start again. It doesn't.

#include <string>
#include <iostream>

using namespace std;

int main()
{
    int q5_answer = 1;
    int q5_answerU;
    int total_score;
    bool q5_valid = false;

    //Question_5:
    do
    {
        cout << "Question 5 (#1 is correct)" <<endl;
        cout << "1.) Answer 1" <<endl;
        cout << "2.) Answer 2" <<endl;
        cout << "3.) Answer 3" <<endl;
        cout << "4.) Answer 4" <<endl;
        cin >> q5_answerU;

        switch (q5_answerU)
        {
        case 1:
            cout << "Correct!" <<endl;
            q5_valid = true;
            (total_score = total_score + 1);
            break;
        case 2:
            cout << "Incorrect" <<endl;
            q5_valid = true;
            break;
        case 3:
            cout << "Incorrect" <<endl;
            q5_valid = true;
            break;
        case 4:
            cout << "Incorrect" <<endl;
            q5_valid = true;
            break;
        default:
            cout << "Invalid answer" <<endl;
        }
    } while (q5_valid = false);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦罢 2024-11-17 16:49:55

while 内的条件是“意外”赋值,而不是比较。它需要读取 while (q5_valid == false) 或更惯用的 while (!q5_valid)

The condition inside the while is an "accidental" assignment, not a comparison. It needs to read while (q5_valid == false) or more idiomatic while (!q5_valid).

梦言归人 2024-11-17 16:49:55

这不是布尔表达式:

q5_valid = false

它将变量 q5_valid 设置为 false 并返回 false。将其替换为:

!q5_valid

q5_valid == false

哪些是相同的。前者的可读性更好。

This is not a boolean expression:

q5_valid = false

It sets the variable q5_valid to false and also returns false. Replace it by:

!q5_valid

or

q5_valid == false

Which are the same. The former is better for readability.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文