| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 4166 人关注过本帖
标题:关于switch中的default,为什么我定义的default不起作用啊
只看楼主 加入收藏
lusan168
Rank: 1
来 自:重庆
等 级:新手上路
帖 子:50
专家分:0
注 册:2007-11-17
收藏
 问题点数:0 回复次数:10 
关于switch中的default,为什么我定义的default不起作用啊
#include<stdio.h>
void main()
{
 int year,month,day;
 int leap;
 printf("please input year,month,day:");
 scanf("%d%d%d",&year,&month,&day);
 switch(month)
 {
  case 12:
   day+=30;
  case 11:
   day+=31;
  case 10:
   day+=30;
  case 9:
   day+=31;
  case 8:
   day+=31;
  case 7:
   day+=30;
  case 6:
   day+=31;
  case 5:
   day+=30;
  case 4:
   day+=31;
  case 3:
   day+=28;
  case 2:
   day+=31;
  case 1:
   day=day;
  default:
   printf("please input error:\n");
   break;
 }
   
 if((year%400==0 || year%4==0 &&year%100!=0) && month>2)
  leap=1;
 else
  leap=0;
 if(leap=1 && month>2)
  day++;
 printf("\nday=%d\n",day);
  
}
搜索更多相关主题的帖子: switch default 定义 
2007-11-26 23:23
cosdos
Rank: 9Rank: 9Rank: 9
来 自:ShangHai
等 级:蜘蛛侠
威 望:6
帖 子:2109
专家分:1385
注 册:2007-6-19
收藏
得分:0 
/* 12个月的天数因该放在数组中 */

因该使用数组

[[italic] 本帖最后由 cosdos 于 2007-11-27 00:02 编辑 [/italic]]

—>〉Sun〈<—
2007-11-26 23:31
cosdos
Rank: 9Rank: 9Rank: 9
来 自:ShangHai
等 级:蜘蛛侠
威 望:6
帖 子:2109
专家分:1385
注 册:2007-6-19
收藏
得分:0 
/*  switch  的修改 */
    switch(month)
    {
        case 12: day+=30;
        case 11: day+=31;
        case 10: day+=30;
        case 9:  day+=31;
        case 8:  day+=31;
        case 7:  day+=30;
        case 6:  day+=31;
        case 5:  day+=30;
        case 4:  day+=31;
        case 3:  day+=28;
        case 2:  day+=31;
        case 1:  day=day; break;
        default: printf("please input error:\n"); break;
    }

—>〉Sun〈<—
2007-11-26 23:35
lusan168
Rank: 1
来 自:重庆
等 级:新手上路
帖 子:50
专家分:0
注 册:2007-11-17
收藏
得分:0 
回复 1# 的帖子
#include<stdio.h>
void main()
{
    int year,month,day;
    int leap;
    printf("please input year,month,day:");
    scanf("%d%d%d",&year,&month,&day);
    switch(month)
    {
        case 12:
            day+=30;
        case 11:
            day+=31;
        case 10:
            day+=30;
        case 9:
            day+=31;
        case 8:
            day+=31;
        case 7:
            day+=30;
        case 6:
            day+=31;
        case 5:
            day+=30;
        case 4:
            day+=31;
        case 3:
            day+=28;
        case 2:
            day+=31;
        case 1:
            day=day;break;
        default:
            printf("sorry,please input error!\n");
        
    }
    if(month>=1 && month<=12){
    if((year%400==0 || year%4==0 &&year%100!=0) && month>2)
        leap=1;
    else
        leap=0;
    if(leap=1 && month>2)
        day++;
    printf("\nday=%d\n",day);
    }
    else
        printf("\nbyebye\n");
        
}
2007-11-26 23:42
木北北
Rank: 1
等 级:新手上路
帖 子:79
专家分:0
注 册:2007-11-24
收藏
得分:0 
还是有问题啊。。。
2007-11-26 23:49
木北北
Rank: 1
等 级:新手上路
帖 子:79
专家分:0
注 册:2007-11-24
收藏
得分:0 
没事了  我习惯每次输入加逗号了  呵呵
2007-11-26 23:52
cosdos
Rank: 9Rank: 9Rank: 9
来 自:ShangHai
等 级:蜘蛛侠
威 望:6
帖 子:2109
专家分:1385
注 册:2007-6-19
收藏
得分:0 
/*  我写的使用数组的 */

#include<stdio.h>

int main(void)
{
    int year, month, day, ndays, i;
    int ds[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
   
    printf("Please input year, month, day: ");
    while(scanf("%d %d %d", &year, &month, &day) == 3)
    {
        while(getchar() != '\n');
        
        if(year % 4 == 0)
            ds[1] == 29;
        else
            ds[2] == 28;

        if(year < 0 || month < 1 || month > 12 || day > ds[month - 1])
        {
            printf("Error!\n\nPlease input year,month,day: ");
            continue;
        }
        
        for(i = ndays = 0; i < month - 1; i++)
            ndays += ds[i];
        ndays += day;
        printf(" : %d\n\n", ndays);
        printf("Please input year, month, day: ");
    }

    puts("Done.");
    return 0;
}

—>〉Sun〈<—
2007-11-26 23:57
cosdos
Rank: 9Rank: 9Rank: 9
来 自:ShangHai
等 级:蜘蛛侠
威 望:6
帖 子:2109
专家分:1385
注 册:2007-6-19
收藏
得分:0 
楼主你的程序没有对输入的数据进行验证!

—>〉Sun〈<—
2007-11-27 00:00
qingfengdan
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2007-11-27
收藏
得分:0 
#include<stdio.h>
int main()
{
int year,month,day;
int leap;
printf("please input year,month,day:");
scanf("%d%d%d",&year,&month,&day);
switch(month)
{
  case 12:
   day+=30;
  case 11:
   day+=31;
  case 10:
   day+=30;
  case 9:
   day+=31;
  case 8:
   day+=31;
  case 7:
   day+=30;
  case 6:
   day+=31;
  case 5:
   day+=30;
  case 4:
   day+=31;
  case 3:
   day+=28;
  case 2:
   day+=31;
  case 1:
   day=day;break;
  default:
   printf("please input error.\n");
   break;
}
   
if(year%400==0 || year%4==0 &&year%100!=0)
  leap=1;
else
  leap=0;
if(leap=1 && (month>2&&month<=12))
  {day++;
printf("\nday=%d\n",day);}
  return 0;
}


关于月份中的天数你还需要限制。
2007-11-27 00:04
xinyue_501
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2007-11-26
收藏
得分:0 
帮你看哈
我改了一下,应该可以达到你的目的了:
#include<stdio.h>
void main()
{
int year,month,day;
int leap;
printf("please input year,month,day:");
scanf("%d%d%d",&year,&month,&day);
int i=1;
switch(month)
{
  case 12:
   day+=30;
   printf("day=%d   %d  \n",day,i++);
  case 11:
   day+=31;
   printf("day=%d   %d  \n",day,i++);
  case 10:
   day+=30;
   printf("day=%d   %d  \n",day,i++);
  case 9:
   day+=31;
   printf("day=%d   %d  \n",day,i++);
  case 8:
   day+=31;
   printf("day=%d   %d  \n",day,i++);
  case 7:
   day+=30;
   printf("day=%d   %d  \n",day,i++);
  case 6:
   day+=31;
   printf("day=%d   %d  \n",day,i++);
  case 5:
   day+=30;
   printf("day=%d   %d  \n",day,i++);
  case 4:
   day+=31;
   printf("day=%d   %d  \n",day,i++);
  case 3:
   day+=28;
   printf("day=%d   %d  \n",day,i++);
  case 2:
   day+=31;
   printf("day=%d   %d  \n",day,i++);
  case 1:
   day=day;
   printf("day=%d   %d  \n",day,i++);break;
  default:
   printf("please input error:\n");
   break;
}
   
if(year%400==0 || (year%4==0 &&year%100!=0) )
  leap=1;
else
  leap=0;
if(leap==1 && month>2)
  day++;
printf("\nday=%d\n",day);
  
}
2007-11-27 00:13
快速回复:关于switch中的default,为什么我定义的default不起作用啊
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.037263 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved