鱼C论坛

 找回密码
 立即注册
查看: 3013|回复: 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;
                }
        }
}
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

  2. int main()  // 请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续判断第二个字母。
  3. {
  4.         char ch;
  5.         printf("please input the first letter of someday!\n");
  6.         while ((ch = getchar()) != 'y')
  7.         {
  8.                 fflush(stdin);
  9.                 switch (ch)
  10.                 {
  11.                         case 'm':
  12.                                 printf("monday\n");
  13.                                 printf("please input the first letter of someday!\n");break;
  14.                         case 'f':
  15.                                 printf("friday\n");
  16.                                 printf("please input the first letter of someday!\n");break;
  17.                         case 'w':
  18.                                 printf("wednesday\n");
  19.                                 printf("please input the first letter of someday!\n");break;
  20.                         case 't':
  21.                                 printf("please input the second letter of someday!\n");
  22.                                 if ( (ch = getchar()) == 'u')
  23.                                         printf("tuesday\n");
  24.                                 else if (ch == 'h')
  25.                                         printf("thursday\n");
  26.                                 else
  27.                                         printf("input error!!!\n");
  28.                                 printf("please input the first letter of someday!\n");
  29.                                 break;
  30.                         case 's':
  31.                                 printf("please input the second letter of someday!\n");
  32.                                 if ((ch = getchar()) == 'a')
  33.                                         printf("saturday\n");
  34.                                 else if (ch == 'u')
  35.                                         printf("sunday\n");
  36.                                 else
  37.                                         printf("input error!!!\n");
  38.                                 printf("please input the first letter of someday!\n");
  39.                                 break;
  40.                 }
  41.         }
  42.        
  43.         return 0;
  44. }
复制代码
写代码,至少用 C11 的标准,int main() return 0;
多重输入还是用 scanf() 或者 scanf_s()比较安全。问题与输入缓冲区有关,所以前面 fflush(stdin) 把缓冲区清空。 if () else if() 是连续的,会执行 if() 的条件判断,如果不符合,执行下一个 else if() 的判断。
getchar() 返回当前指针指向缓冲区的字符,然后指针完后移动。你输入的只有单一字符,所以下一个是回车。你用两个 getchar() 就指向换行了。
getchar() 的功能不是要求输入,而是从输入缓冲区里提出一个元素。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

好的 非常感谢您!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

貌似这样修改还是不行啊,运行起来结果比没改之前还糟糕,怎么破?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

你直接用我的代码吗?
  1. please input the first letter of someday!
  2. m
  3. monday
  4. please input the first letter of someday!
  5. f
  6. friday
  7. please input the first letter of someday!
  8. w
  9. wednesday
  10. please input the first letter of someday!
  11. t
  12. please input the second letter of someday!
  13. u
  14. tuesday
  15. please input the first letter of someday!
  16. t
  17. please input the second letter of someday!
  18. h
  19. thursday
  20. please input the first letter of someday!
  21. s
  22. please input the second letter of someday!
  23. a
  24. saturday
  25. please input the first letter of someday!
  26. s
  27. please input the second letter of someday!
  28. u
  29. sunday
  30. please input the first letter of someday!
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-10 13:15

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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