c - 终止C语言程序

标签 c

有人可以帮我修改我的代码吗? 我的程序没有终止。选择品牌后,它应该终止,但它会永远循环。

#include<stdio.h>
int main (void)
{
    int number,count=0 ;

    while (count<3)
    {
        printf("Menu:\n");
        printf("1.Proace\n");
        printf("2.Yonex\n");
        printf("3.Reebook\n");
        printf("0.Exit\n");
        printf("Enter your selection:");
        scanf("%d",&number);

        switch (number)
        {
        case 0:
            printf("exit.\n");
            break;
        case 1:
            printf("You have selected proace.\n");
            break;
        case 2:
            printf("You have selected yonex.\n");
            break;
        case 3:
            printf("You have selected reebook.\n");
            break;

        default:
            printf("Please try again.\n");
            exit(0);
        }
    }
    return 0;
}

最佳答案

我猜你误解了 switchdefault 情况:只有当 number 不是 0,1,2,3 时才会执行它。 如果您想每次 case 0 1 2 3

退出一次 exit(0)
#include<stdio.h>
int main (void)
{
    int number,count=0 ;

    while (count<3)
    {
        printf("Menu:\n");
        printf("1.Proace\n");
        printf("2.Yonex\n");
        printf("3.Reebook\n");
        printf("0.Exit\n");
        printf("Enter your selection:");
        scanf("%d",&number);

        switch (number)
        {
        case 0:
            printf("exit.\n");
            exit(0);
            break;
        case 1:
            printf("You have selected proace.\n");
            exit(0);
            break;
        case 2:
            printf("You have selected yonex.\n");
            exit(0);
            break;
        case 3:
            printf("You have selected reebook.\n");
            exit(0);
            break;

        default:
            printf("Please try again.\n");
        }
    }
    return 0;
}

顺便说一句,更好的代码应该是

#include<stdio.h>
int main (void)
{
    int number;
    bool canExit = false;

    while (canExit == false)
    {
       canExit  = true;

        printf("Menu:\n");
        printf("1.Proace\n");
        printf("2.Yonex\n");
        printf("3.Reebook\n");
        printf("0.Exit\n");
        printf("Enter your selection:");
        scanf("%d",&number);

        switch (number)
        {
        case 0:
            printf("exit.\n");
            break;
        case 1:
            printf("You have selected proace.\n");
            break;
        case 2:
            printf("You have selected yonex.\n");
            break;
        case 3:
            printf("You have selected reebook.\n");
            break;

        default:
            printf("Please try again.\n");
            canExit = false;
        }
    }
    return 0;
}

关于c - 终止C语言程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32602094/

相关文章:

c - 如何利用 Format-String 漏洞?

c - 反对作废*

c - 如何在 C 中哈希 CPU ID

c - C 中动态参数传递给 execlp() 函数

c - 为什么要跳过 void 函数调用而不捕获它们?

c - 在 C 中返回多个文件指针

c++ - 检查 BOOL 的值是否比更新它更有效?

c - DispatchReadSource 事件处理程序未针对绑定(bind)套接字触发

c - 如何在 C 程序中从命令行打开和读取二进制文件

c - 即使未指定返回变量或值,C 编译器如何能够准确地猜测应该返回哪个值?