我如何逃避开关和while(1)循环?

问题描述 投票:0回答:1

谢谢您的光临。这是从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突破底部会让我逃脱,但没有用。我将不胜感激。

c++ loops while-loop switch-statement escaping
1个回答
0
投票

您可以使用2个嵌套的while循环来做到这一点:

int main(){

while(1)
{
    srand(time(0));
    printf("Making a random problem. \n");
    int x = rand() % 100;
    int y = rand() % 100;
    int op = rand() % 4;

    bool bContinueAskTheProblem = true;

    while (bContinueAskTheProblem)
    {
        int ans;

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

                printf("correct.\n");
                bContinueAskTheProblem = false;
            }
            else
                printf("wrong.\n");
        case 2:
            printf("%d * %d = ", x, y);
            scanf("%d", &ans);
            if (x * y == ans)
            {
                printf("correct.\n");
                bContinueAskTheProblem = false;
            }
            else
                printf("wrong.\n");

        case 3:
            printf("%d / %d = ", x, y);
            scanf("%d", &ans);
            if (x / y == ans)
            {
                printf("correct.\n");
                bContinueAskTheProblem = false;
            }
            else
                printf("wrong.\n");
        }
    }
}

return 0;

}


0
投票

我以为* 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 = 2; //rand() % 100;
    int y = 2; //rand() % 100;
    int op = 2; //rand() % 4;
    char op_char;

    int 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;
    }

    while (1) {
        printf("%d %c %d = ", x, op_char, y);
        scanf("%d", &ans);
        if (ans == correct_answer)
        {
            printf("correct.\n");
            break;
        }

        printf("wrong.\n");
    }

    return 0;
}

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

BTW:

从不做

scanf("%d", &ans);

总是检查返回值,如:

if (scanf("%d", &ans) != 1)
{
    // ups - bad input - add error handling
}
© www.soinside.com 2019 - 2024. All rights reserved.