鱼C论坛

 找回密码
 立即注册
查看: 2584|回复: 4

求大神赐教!为什么case‘t’的情况只能出现一个正确的!错在哪了?

[复制链接]
发表于 2018-10-7 15:33:24 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
void main()  // 请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续判断第二个字母。

{
        char ch;
        printf("please input the frist word of someday!\n");
        while ((ch = getchar()) != 'y')
        {
                switch (ch)
                {
                case 'm': printf("monday\n"); break;
                case 'f': printf("firday\n"); break;
                case 'w': printf("wednesday\n"); break;
                case 't': printf("please input the second word of someday!\n");
                        if ((ch = getchar()) == 'u')
                        {
                                printf("tuesday\n");
                        }
                        else if ((ch = getchar()) == 'h')
                        {
                                printf("thursday\n");
                        }
                        else
                                printf("input error!!!\n");
                        break;
                case 's': printf("please input the second word of someday!\n");
                        if ((ch = getchar()) == 'a')
                        {
                        printf("saturday\n");
                        }
                        else if ((ch = getchar()) == 'u')
                        {
                        printf("sunday\n");
                        }
                        else
                        printf("input error!!!\n");
                        break;
                }
        }
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-10-7 16:31:50 | 显示全部楼层
#include<stdio.h>

int main()  // 请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续判断第二个字母。
{
        char ch;
        printf("please input the first letter of someday!\n");
        while ((ch = getchar()) != 'y')
        {
                fflush(stdin);
                switch (ch)
                {
                        case 'm': 
                                printf("monday\n"); 
                                printf("please input the first letter of someday!\n");break;
                        case 'f': 
                                printf("friday\n"); 
                                printf("please input the first letter of someday!\n");break;
                        case 'w': 
                                printf("wednesday\n"); 
                                printf("please input the first letter of someday!\n");break;
                        case 't': 
                                printf("please input the second letter of someday!\n");
                                if ( (ch = getchar()) == 'u')
                                        printf("tuesday\n");
                                else if (ch == 'h')
                                        printf("thursday\n");
                                else
                                        printf("input error!!!\n");
                                printf("please input the first letter of someday!\n");
                                break;
                        case 's': 
                                printf("please input the second letter of someday!\n");
                                if ((ch = getchar()) == 'a')
                                        printf("saturday\n");
                                else if (ch == 'u')
                                        printf("sunday\n");
                                else
                                        printf("input error!!!\n");
                                printf("please input the first letter of someday!\n");
                                break;
                }
        }
        
        return 0;
}
写代码,至少用 C11 的标准,int main() return 0;
多重输入还是用 scanf() 或者 scanf_s()比较安全。问题与输入缓冲区有关,所以前面 fflush(stdin) 把缓冲区清空。 if () else if() 是连续的,会执行 if() 的条件判断,如果不符合,执行下一个 else if() 的判断。
getchar() 返回当前指针指向缓冲区的字符,然后指针完后移动。你输入的只有单一字符,所以下一个是回车。你用两个 getchar() 就指向换行了。
getchar() 的功能不是要求输入,而是从输入缓冲区里提出一个元素。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-10-7 16:38:02 | 显示全部楼层
claws0n 发表于 2018-10-7 16:31
写代码,至少用 C11 的标准,int main() return 0;
多重输入还是用 scanf() 或者 scanf_s()比较安全。问 ...

好的 非常感谢您!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-10-7 16:49:50 | 显示全部楼层
claws0n 发表于 2018-10-7 16:31
写代码,至少用 C11 的标准,int main() return 0;
多重输入还是用 scanf() 或者 scanf_s()比较安全。问 ...

貌似这样修改还是不行啊,运行起来结果比没改之前还糟糕,怎么破?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-10-7 16:54:08 | 显示全部楼层
JuniorHu 发表于 2018-10-7 16:49
貌似这样修改还是不行啊,运行起来结果比没改之前还糟糕,怎么破?

你直接用我的代码吗?
please input the first letter of someday!
m
monday
please input the first letter of someday!
f
friday
please input the first letter of someday!
w
wednesday
please input the first letter of someday!
t
please input the second letter of someday!
u
tuesday
please input the first letter of someday!
t
please input the second letter of someday!
h
thursday
please input the first letter of someday!
s
please input the second letter of someday!
a
saturday
please input the first letter of someday!
s
please input the second letter of someday!
u
sunday
please input the first letter of someday!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-2 16:15

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表