c - switch 语句即使在 break 之后也执行 case 2

标签 c

当情况 1 时,程序会执行预期的代码,但当它询问您是否要再次计算体积时,如果您选择是,它只会执行情况 2。我不确定哪里出了问题。除非您在菜单中选择 2,否则如何让它只执行情况 1?

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

        int main()
        {
            float menu1, opt1, opt2, opt3, opt4, t;
            int td;

            printf("Enter: ");
            scanf("%d",&td);
        switch(td) {
            case 1:
            printf("Enter a, b, c, and h of the triangular prism in meters\n\n");
            printf("a ");
            scanf("%f", &opt1);
            printf("b ");
            scanf("%f", &opt2);
            printf("c ");
            scanf("%f", &opt3);
            printf("h ");
            scanf("%f", &opt4);
            printf("\nWould you like to make another Volume calculation (1 for Yes, 2 for No)?");
            scanf("%f", &menu1);
            if (menu1 == 2) {
                t = 0;
                break;
            }
            if (menu1 < 1 || menu1 > 2) {
                printf("\n\nUser choice must be between 1 and 2!\n\n");
                printf("Would you like to make another Volume calculation (1 for Yes, 2 for No)?");
                scanf("%f", &menu1);
                if(menu1 == 2) {
                   t = 0;
                   break;
                }
            }

        case 2:
            printf("Enter a and h of the triangular pyramid\n\n");
            printf("a ");
            scanf("%f", &opt1);
            printf("h ");
            scanf("%f", &opt2);
            printf("\nWould you like to make another Volume calculation (1 for Yes, 2 for No)?");
            scanf("%f", &menu1);
            if (menu1 == 2) {
                t = 0;
                break;
            }
            if (menu1 < 1 || menu1 > 2) {
                printf("\n\nUser choice must be between 1 and 2!\n\n");
                printf("Would you like to make another Volume calculation (1 for Yes, 2 for No)?");
                scanf("%f", &menu1);
                if(menu1 == 2) {
                   t = 0;
                   break;
                }
            }
    }
        }

最佳答案

您的 break 语句 [for case 1:] if block 中。如果 if 都不是 true,那么您就失败了。

这是你的原作:

case 1:
    // ...
    if (menu1 < 1 || (menu1 > 2) {
        // ...
        if (menu1 == 2) {
            t = 0;
            break;
        }
    }
    // BUG: there should be a break here

case 2:
    // ...
    printf("Enter a and h of the triangular pyramid\n\n");
    printf("a ");
    // ...

固定代码如下:

case 1:
    // ...
    if (menu1 < 1 || (menu1 > 2) {
        // ...
        if (menu1 == 2) {
            t = 0;
            break;
        }
    }
    break;  // FIX: this is your _missing_ break statement

case 2:
    // ...
    printf("Enter a and h of the triangular pyramid\n\n");
    printf("a ");
    // ...

关于c - switch 语句即使在 break 之后也执行 case 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36683296/

相关文章:

将 C 语言转换为 MIPS 汇编语言

c - 当 `GC.enable(true)` 时,在 Julia 中使用 ccall 时出现段错误

c - 从原始文件读取时出现段错误

php - 在某些情况下无法从 PHP 停止 C 守护进程

c - 这个半反编译代码有什么作用(C)

c - 从 C 函数返回字符串

c - 我正在尝试编写一个 C 代码来对 String 进行排序,但第 13 行总是显示错误消息

c - 使用 printf 的可变长度空间

c++ - 如果两个数字都指向同一位置,则在不使用额外空间的情况下交换两个数字不起作用?

在 C 中跨多个源文件创建调度表注册函数