C语言编程,同时做带开关的情况下的程序 [英] C programming do while with switch case program

查看:224
本文介绍了C语言编程,同时做带开关的情况下的程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经能够做到开关箱的计划,但我想,直到用户选择退出计划一次又一次运行。
我基本上希望计划一次又一次地用做运行while循环...

 开关(I)
{
情况1:
    的printf(67);
    打破;
案例2:
    的printf(45);
    打破;
默认:
    的printf(默认);
}


解决方案

使用做...而循环是这样的:

  INT I = 1; //初始化一些非零数字为prevent UB
的printf(请输入0退出\\ n);
做{
    如果(scanf的(%d个,&放大器;!我)= 1)//如果诸如字符无效数据被输入
    {
        scanf函数(%* [^ \\ n]);
        scanf函数(%* C); //清除标准输入
    }
}而(我!= 0); //循环直到`I`不为0

这件code将循环,直到用户输入0。您可以根据自己的需要更改这个code。如果你希望你的开关在此,复制您的张贴code后 scanf函数

I have been able to do switch case program but I want program to run again and again until a user selects to quit. I basically wants program to run again and again using do while loop...

switch(I)
{
case 1:
    printf("67");
    break;
case 2:
    printf("45");
    break;
default:
    printf("default");
}

解决方案

Use a do...while loop like this:

int I = 1; //Initialize to some non-zero number to prevent UB
printf("Enter 0 to quit \n");
do{
    if (scanf("%d",&I) != 1) //If invalid data such as characters are inputted
    {
        scanf("%*[^\n]");
        scanf("%*c");    //Clear the stdin
    }
} while(I!=0); //Loop until `I` is not 0 

This piece of code will loop until the user enters 0. You can change this code according to your needs. If you want your switch in this, copy your posted code after the scanf.

这篇关于C语言编程,同时做带开关的情况下的程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆