|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
大佬们为啥这一段switch没有生效啊,直接就进入下次循环了
这是缩减版代码:
- #include <stdio.h>
- float jiafa(float opr1, float opr2);
- int main()
- {
- int on_off;
- char select, buttom;
- float opr1, opr2, outcome;
-
- printf("请选择你要的功能:\n");
- printf("*************************************\n");
- printf("a:加法\t\tb:减法\n");
- printf("c:乘法\t\td:除法\n");
- printf("q:退出\n");
- printf("*************************************\n");
- while(scanf("%c", &select)){
- switch(select){
- case 'a':
- on_off = 1;
- while(on_off){
- while(1){
- printf("请输入第一个操作数:");
- if(scanf("%f", &opr1) == 0){
- getchar();
- printf("请输入正确的数字!\n");
- printf("再次输入!");
- continue;}
- else break;}
- while(1){
- printf("请输入第二个操作数:");
- if(scanf("%f", &opr2) == 0){
- getchar();
- printf("请输入正确的数字!\n");
- printf("再次输入!");
- continue;}
- else break;}
- outcome = jiafa(opr1, opr2);
- printf("结果是:%.2f", outcome);
- printf("是否继续,按y继续,按n返回主菜单:");
- scanf("%c", &buttom);
- switch(buttom){
- case 'y':printf("请输入一次组计算数据\n");break;
- case 'n':on_off = 0;break;
- default:printf("请输入正确选项!\n");continue;
- }
- }break;}
- printf("再见!");
-
- return 0;
- }
- }
-
- float jiafa(float opr1, float opr2){
- float output;
- output = opr1+opr2;
- return output;
- }
复制代码
scanf使用%c读取字符的时候,不会跳过空白符,直接读取缓冲区中的第一个字符。
因为你的程序之前读取过其他内容,因此会在缓冲器留下分隔字符(即空白符),所以这里你再次读取%c的时候就会读取到'\n'
|
|