问答详情
源自:4-17 switch与if语句的应用

请问这个代码哪里错了啊?

#include <stdio.h>
int main()
{
    /* 定义需要计算的日期 */
    int year = 2008;
    int month = 8;
    int day = 8;
    /*
     * 请使用switch语句,if...else语句完成本题
     * 如有想看小编思路的,可以点击左侧任务中的“不会了怎么办”
     * 小编还是希望大家独立完成哦~
     */
    int sum,flag;
    switch(month)
    {
        case 1:sum=0;break;   
        case 2:sum=31;break;
        case 3:sum+=28;break;       
        case 4:sum+=31;break;
        case 5:sum+=30;break;
        case 6:sum+=31;break;
        case 7:sum+=30;break;
        case 8:sum+=31;break;
        case 9:sum+=30;break;
        case 10:sum+=31;break;
        case 11:sum+=30;break;
        case 12:sum+=31;break;
        default: printf("一年只有十二个月");break;
    }
    sum=sum+day;
    if(year%400==0||year%4==0 &&year%100!=0)
       flag=1;
    else
       flag=0;
    if(flag==1&&month>2)  
      sum+=1;
      printf("%d年%d月%d日是该年中的第%d天",year, month, day, sum);
   
    return 0;
}  

提问者:慕仰029879 2018-08-27 21:56

个回答

  • XpG12138
    2018-08-28 00:25:09
    已采纳

    没有for循环,要把前几个月份的天数累加,应该在switch外套个for循环,请参考。

    #include <stdio.h>
    int main()
     {     /* 定义需要计算的日期 */    
         int year = 2008;    
         int month = 8;   
          int  day=8,day1= 8,i;       
           /*     * 请使用switch语句,if...else语句完成本题     
           * 如有想看小编思路的,可以点击左侧任务中的“不会了怎么办”   
             * 小编还是希望大家独立完成哦~     */     
             for(i=1;i<month;i++)     
             {        
                 switch(i)        
                     {            
                         case 1:day+=31;                    
                         break;              
                         case 2:            
                         if ((year%4==0&&year%100!=0)||year%400==0)                
                             day+=29;            
                         else                 
                             day+=28;                
                             break;            
                          case 3:day+=31;                    
                          break;                    
                          case 4:day+=30;                    
                          break;            
                          case 5:day+=31;                    
                          break;            
                          case 6:day+=30;                    
                          break;            
                          case 7:day+=31;                    
                          break;            
                          case 8:day+=31;                    
                          break;            
                          case 9:day+=30;                    
                          break;            
                          case 10:day+=31;                   
                           break;            
                           case 11:day+=30;                   
                           break;            
                           case 12:day+=31;                    
                           break;        
                       }    
               }     
               printf("%d年%d月%d日是该年的第%d天",year,month,day1,day); 	
               return 0;
     }