【问题标题】:How can I escape the switch and while(1) loop?如何逃脱 switch 和 while(1) 循环?
【发布时间】:2020-08-26 13:58:15
【问题描述】:

感谢您过来看看这个。这是由 Visual c++ 制作的。

我想用随机数和算术运算做一个随机问题。

它应该给我一个新的问题,直到我得到正确的答案,当我得到正确的答案时,它应该被停止并关闭。但是,即使我得到了正确的答案,它也不会让我摆脱循环,而是不断给我一个新问题。请查看我制作的以下代码。


#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(){
    int ans;
    srand(time(0));
    printf("Making a random problem. \n");

    int x = rand() % 100;
    int y = rand() % 100;
    int op = rand() % 4;

    while (1) {

        switch (op)
        {
        case 0:
            printf("%d + %d = ", x, y);
            scanf("%d", &ans);
            if (x + y == ans)
            {
                printf("correct.\n");
                break;
            }
            else
                printf("wrong.\n");
        case 1:
            printf("%d - %d = ", x, y);
            scanf("%d", &ans);
            if (x - y == ans)
            {
                printf("correct.\n");
                break;
            }
            else
                printf("wrong.\n");
        case 2:
            printf("%d * %d = ", x, y);
            scanf("%d", &ans);
            if (x * y == ans)
            {
                printf("correct.\n");
                break;
            }
            else
                printf("wrong.\n");
        case 3:
            printf("%d / %d = ", x, y);
            scanf("%d", &ans);
            if (x / y == ans)
            {
                printf("correct.\n");
                break;
            }
            else
                printf("wrong.\n");
        }
        break; //*1
    }

    return 0;
}

当我得到正确答案时,你能告诉我如何摆脱循环吗? 我认为底部的 *1 突破会让我逃脱,但它没有用。我会提前感谢它。

【问题讨论】:

  • 您可以拨打continue;
  • breakswitch 之后/之外将在任何情况下离开您的while 循环。如果您想防止这种情况发生,请使用continue 结束所有情况(包括default:)以跳过此(当然,case 1 除外)。
  • 我将此类问题视为来自数据提供者的消息,即您将太多内容打包到一个函数中。如果将循环和开关移动到另一个函数中,您可以在想要退出循环时return
  • 一个更易于维护的变体是使用最初设置为false的标志(例如bool变量),检查while条件并仅设置为true如果你想离开循环。
  • 注意:无论您使用什么学习材料,似乎都在教您 C,而不是 C++。两种语言之间有一些相当大的区别。

标签: c++ loops while-loop switch-statement escaping


【解决方案1】:

我认为底部周围的 *1 突破会让我逃脱,但它没有用。我会提前感谢它。

它会打破循环。但它会打破错误和正确答案的循环(这不是你想要的)。

我认为您对 switch-case 语句中的另一个错误感到困惑。对于每个案例,您都需要在案例的末尾放置中断。如果您不这样做,则继续执行下一个案例。所以:

    case 0:
        printf("%d + %d = ", x, y);
        scanf("%d", &ans);
        if (x + y == ans)
        {
            printf("correct.\n");
            break;
        }
        else
            printf("wrong.\n");

应该是

    case 0:
        printf("%d + %d = ", x, y);
        scanf("%d", &ans);
        if (x + y == ans)
        {
            printf("correct.\n");
        }
        else
        {
            printf("wrong.\n");
        }
        break;

或者更紧凑一点:

    case 0:
        printf("%d + %d = ", x, y);
        scanf("%d", &ans);
        printf("%s.\n", (x + y == ans) ? "correct" : "wrong");
        break;

回到原来的问题:

如何打破while循环只有在答案正确时

有很多方法可以解决这个问题。一个经典的解决方案是使用标志,即代替while(1) 执行bool answer_wrong = true; while(answer_wrong) { .. }; 之类的操作,然后在得到正确答案时更改answer_wrong

这是针对您的特定问题的另一种解决方案:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(){
    int ans;
    srand(time(0));
    printf("Making a random problem. \n");

    int x = rand() % 100;
    int y = rand() % 100;
    int op = rand() % 4;

    char op_char;        
    int correct_answer;

    // Calculate the correct answer
    switch (op)
    {
        case 0:
            correct_answer = x + y;
            op_char = '+';
            break;
        case 1:
            correct_answer = x - y;
            op_char = '-';
            break;
        case 2:
            correct_answer = x * y;
            op_char = '*';
            break;
        case 3:
            correct_answer = x / y;
            op_char = '/';
            break;
    }

    // Keep looping till you get the correct answer
    while (1) {
        printf("%d %c %d = ", x, op_char, y);
        scanf("%d", &ans);
        if (ans == correct_answer)
        {
            printf("correct.\n");

            // Answer correct so break the while loop
            break;
        }

        printf("wrong.\n");
    }

    return 0;
}

这里的用户输入是在switch 语句之外进行的,因此当答案正确时,您可以立即跳出while

顺便说一句:

永远不要这样做

scanf("%d", &ans);

始终检查返回值,例如:

if (scanf("%d", &ans) != 1)
{
    // ups - bad input - add error handling
}

【讨论】:

    猜你喜欢
    • 2016-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-07
    • 2013-12-20
    • 1970-01-01
    • 1970-01-01
    • 2012-03-13
    相关资源
    最近更新 更多