鱼C论坛

 找回密码
 立即注册
查看: 1552|回复: 6

错在哪???

[复制链接]
发表于 2016-10-13 16:57:07 | 显示全部楼层 |阅读模式
1鱼币
  1. /*
  2. 题目:请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,
  3. 则继续判断第二个字母。
  4. 1.程序分析:用情况语句比较好,如果第一个字母一样,则判断用情况语句或 if
  5. 语句判断第二个字母。
  6. */

  7. #include<stdio.h>

  8. int main()
  9. {
  10.         char letter;
  11.         printf("please input the first letter of oneday!\n");

  12.         while((letter=getchar())!='Y')         //改为‘y’
  13.         {
  14.                 switch(letter)
  15.                 {
  16.                 case 'S':printf("please input second letter\n");
  17.                         if((letter=getchar())=='a')
  18.                         {
  19.                                 printf("saturday\n");
  20.                         }
  21.                         else if((letter=getchar())=='u')
  22.                         {
  23.                                 printf("sunday\n");
  24.                         }
  25.                         else
  26.                         {
  27.                                 printf("data error!\n");
  28.                         }
  29.                         break;

  30.                 case 'F':printf("friday\n");
  31.                         break;

  32.                 case 'M':printf("monday\n");
  33.                         break;

  34.                 case 'T':printf("please input the second letter\n");
  35.                         if((letter=getchar())=='u')
  36.                         {
  37.                                 printf("tuesday\n");
  38.                         }
  39.                         else if((letter=getchar())=='h')
  40.                         {
  41.                                 printf("thursday\n");
  42.                         }
  43.                         else
  44.                         {
  45.                                 printf("data error\n");
  46.                         }
  47.                         break;

  48.                 case 'W':printf("wednesday\n");
  49.                         break;

  50.                 default:printf("data error!\n");
  51.                 }
  52.         }
  53. }
复制代码


QQ截图20161013165411.jpg

最佳答案

查看完整内容

满意请评分打赏
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-10-13 16:57:08 | 显示全部楼层
  1. #include<stdio.h>

  2. int main()
  3. {
  4.         char letter;
  5.         printf("please input the first letter of oneday!\n");

  6.         while((letter=getchar())!='Y')         //改为‘y’
  7.         {
  8.             getchar();
  9.                 switch(letter)
  10.                 {
  11.                 case 'S':printf("please input second letter\n");
  12.                         if((letter=getchar())=='a')
  13.                         {
  14.                                 printf("saturday\n");
  15.                         }
  16.                         else if(letter=='u')
  17.                         {
  18.                                 printf("sunday\n");
  19.                         }
  20.                         else
  21.                         {
  22.                                 printf("data error!\n");
  23.                         }
  24.                         getchar();
  25.                         break;

  26.                 case 'F':printf("friday\n");
  27.                         break;

  28.                 case 'M':printf("monday\n");
  29.                         break;

  30.                 case 'T':printf("please input the second letter\n");
  31.                         if((letter=getchar())=='u')
  32.                         {
  33.                                 printf("tuesday\n");
  34.                         }
  35.                         else if(letter=='h')
  36.                         {
  37.                                 printf("thursday\n");
  38.                         }
  39.                         else
  40.                         {
  41.                                 printf("data error\n");
  42.                         }
  43.                         getchar();
  44.                         break;

  45.                 case 'W':printf("wednesday\n");
  46.                         break;

  47.                 default:printf("data error!\n");
  48.                 }
  49.         }
  50. }
复制代码




满意请评分打赏
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-10-13 18:36:03 | 显示全部楼层
本帖最后由 orino 于 2016-10-17 08:22 编辑

有不少问题,第一,在使用l=getchar()之后,按回车键是会产生换行回车两个字符,回车输入字符后还会剩下一个换行字符留在输入区,这时马上用l=getchar()是会捕获这个换行字符而不是你想输入的字符,所以如果一定要每次输入后按回车键,那么就请在每次使用后单独用一个getchar()吞掉这个多余的字符;第二点,照你的第二个字母判断方法来看,你的else if里的字符在第一次输入时都不会被匹配,所以推荐在每个case里只用一个l=letchar(),然后对l判断.
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-10-13 18:59:22 | 显示全部楼层
本帖最后由 huabcdf 于 2016-10-13 19:01 编辑

二个问题,一是没过滤掉加车,二是判断时不能重复从缓冲区提取字符。
更改如下,若有不对,请指正。
#include<stdio.h>

int main()
{
        char letter;
        printf("please input the first letter of oneday!\n");

        while((letter=getchar())!='Y')         //改为‘y’
        {
                fflush(stdin);//清空缓冲区
                switch(letter)
                {
                case 'S':printf("please input second letter\n");
                         letter=getchar();//只能从缓冲区提取一次字符
                        //  fflush(stdin);//清空缓冲区
                        if(letter=='a')
                        {
                                printf("saturday\n");
                        }
                        else if(letter=='u')
                        {
                                printf("sunday\n");
                        }
                        else
                        {
                                printf("data error!\n");
                        }
                        break;

                case 'F':printf("friday\n");
                        break;

                case 'M':printf("monday\n");
                        break;

                case 'T':printf("please input the second letter\n");
                        letter=getchar();
                        //  fflush(stdin);//清空缓冲区
                        if(letter=='u')
                        {
                                printf("tuesday\n");
                        }
                        else if(letter=='h')
                        {
                                printf("thursday\n");
                        }
                        else
                        {
                                printf("data error\n");
                        }
                        break;

                case 'W':printf("wednesday\n");
                        break;
                case '\n':break;//输入第二个字符时若不清空缓冲区会把回车读到下轮的letter.

                default:printf("data error!\n");
                }
        }
}
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2016-10-15 15:01:13 | 显示全部楼层
orino 发表于 2016-10-13 16:57
满意请评分打赏

收到鱼币了吗?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2016-10-15 15:03:20 | 显示全部楼层
huabcdf 发表于 2016-10-13 18:59
二个问题,一是没过滤掉加车,二是判断时不能重复从缓冲区提取字符。
更改如下,若有不对,请指正。
#inc ...

谢谢!!!

网上说fflush(stdin);此函数仅适用于部分编译器(如VC6)
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-10-15 16:33:09 | 显示全部楼层
本帖最后由 orino 于 2016-10-17 08:21 编辑

嗯,原来说的顺序反了,准确的是先回车(13,\r)再换行(10,\n)已经修正了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-10 14:39

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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