鱼C论坛

 找回密码
 立即注册
查看: 3106|回复: 5

c语言求助!!!

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

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

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

x
题目:
请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续
    判断第二个字母。


为什么我每次输入t或者s的时候就会出问题。输出的不是要的结果呢!
#include <stdio.h>
void main()
{
    char c, d;
    printf("please input the first letter !\n");
    c = getchar();
    if( (c=='m') || (c=='M') )
            printf("Monday\n");
    if( (c=='f') || (c=='F') )
            printf("Friday\n");
    if( (c=='w') || (c=='W') )
            printf("Wednesday\n");
    while( (c=='T') || (c=='t') )
    {
        printf("please input the second letter !\n");        
        if( (c=getchar())=='u' || (c=getchar())=='U')
        {
            printf("Tuesday\n");
        }
        else
            printf("Thursday\n");
    }
    while( (c=='s') || (c=='S') )
    {
        printf("please input the second letter !\n");
        if( (c=getchar())=='a' || (c=getchar())=='A' )
        {
            printf("Saturday\n");
        }
        else
            printf("Sunday\n");
    }
   
}
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2012-7-15 20:25:54 | 显示全部楼层
  1. #include <stdio.h>
  2. #include<stdlib.h>

  3. int main()
  4. {
  5.         char c;                                // 定义的局部变量没有被引用到  所以不需要定义咯吧
  6.         printf("please input the first letter !\n");
  7.         c = getchar();
  8.         if((c=='m'||c=='M')||( (c=='f') || (c=='F') )||( (c=='w') || (c=='W') )||( (c=='T') || (c=='t') )||( (c=='s') || (c=='S') ))
  9.         {
  10.                 if( (c=='m') || (c=='M') )
  11.                         printf("Monday\n");
  12.                 else if( (c=='f') || (c=='F') )
  13.                         printf("Friday\n");
  14.                 else if( (c=='w') || (c=='W') )
  15.                         printf("Wednesday\n");
  16.                 else if( (c=='T') || (c=='t') )
  17.                 {
  18.                         getchar();
  19.                         printf("please input the second letter !\n");        
  20.                         if( (c=getchar())=='u' || (c=getchar())=='U')
  21.                         {
  22.                                 printf("Tuesday\n");
  23.                         }
  24.                         else
  25.                                 printf("Thursday\n");
  26.                 }
  27.                 else if( (c=='s') || (c=='S') )
  28.                 {
  29.                         printf("please input the second letter !\n");
  30.                         getchar();
  31.                         if( (c=getchar())=='a' || (c=getchar())=='A' )
  32.                         {
  33.                                 printf("Saturday\n");
  34.                         }
  35.                         else
  36.                                 printf("Sunday\n");
  37.                 }
  38.         }
  39.         else
  40.         {
  41.                 printf("输入的字符不符合!\n");
  42.                 exit(0);
  43.         }
  44.         return 0;
  45. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2012-7-15 20:27:57 | 显示全部楼层
你程序的 变量d 没有被用到 所以不用定义了:lol 上面没写清楚  你主要错误  就是 在第二次 输入的时候 没有把 缓冲区里面的 回车 清除掉 所以 在第二次 用c字符 接收的 时候 其实是 接收到了 回车符   你只要在第二次输入的时候  用 getchar() 过滤 一下 缓冲区 就可以咯:lol
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2012-7-16 00:04:11 | 显示全部楼层
输出的不是要的结果呢!为什么
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2012-7-16 19:04:58 | 显示全部楼层
本帖最后由 郑小夕 于 2012-7-16 19:07 编辑


这样也有问题。
if( (c=getchar())=='u' || (c=getchar())=='U')
if( (c=getchar())=='a' || (c=getchar())=='A' )
娶两次干嘛?
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2012-7-16 20:37:48 | 显示全部楼层
郑小夕 发表于 2012-7-16 19:04
这样也有问题。
if( (c=getchar())=='u' || (c=getchar())=='U')
if( (c=getchar())=='a' || (c=getc ...

这是输入 大写 或者小写 都可以 0.0
小甲鱼最新课程 -> https://ilovefishc.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2025-11-14 04:35

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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