首页 > 用户发贴区 > 编程问题提问区 > 求教!加入循环后出现逻辑错误
2007
12-23

求教!加入循环后出现逻辑错误

main()
{
      int day,month,year,sum,leap;
      
      loop :
     
      printf(“请输入年,月,日,如1986,4,21\n”);
     
      scanf(“%d,%d,%d”,&year,&month,&day);
     
      if(year>=0)
      {
                 if(month>0&&month<13)
                 {
                                      if(month==1&&month==3&&month==5&&month==7&&month==8&&month==10&&month==12)
                                      {if(day>32)
                                          {printf(“号数错误,请检查后重新输入\n”);
                                          goto loop;}
                                          }
                                      
                                      else if(month=2)
                                      {if(day>29)
                                          {printf(“号数错误,请检查后重新输入\n”);
                                          goto loop;}
                                          }
                                      else if(month==4&&month==6&&month==9&&month==11)
                                      {if(day>30)
                                          {printf(“号数错误,请检查后重新输入\n”);
                                          goto loop;}
                                          }
                 }
                 else{
                      printf(“月数输入错误,请检查后重新输入\n”);
                      goto loop;
                      }
                      }
      else
      {
           printf(“年数输入错误,请检查后重新输入\n”);
           goto loop;
           }
                            
     
      switch(month)
     
      {
                   case 1 : sum=0;  break;
                   case 2 : sum=31; break;
                   case 3 : sum=59; break;
                   case 4 : sum=90; break;
                   case 5 : sum=120;break;
                   case 6 : sum=151;break;
                   case 7 : sum=181;break;
                   case 8 : sum=212;break;
                   case 9 : sum=243;break;
                   case 10: sum=273;break;
                   case 11: sum=304;break;
                   case 12: sum=334;break;
                  
                   }
     
      sum=sum+day;
     
      if(year%400==0||(year%4==0&&year%100!=0))
          leap=1;
         
      else
          leap=0;
         
      if(leap=1&&month>2)
          sum++;
         
      printf(“是%d年中的第%d天\n”,year,sum);
     
      system(“pause”);
      }       


求教!加入循环后出现逻辑错误》有 5 条评论

  1. wsgc01 说:

    if(year>=0)
          {
                     if(month>0&&month<13)
                     {
                                         
    if(month==1&&month==3&&month==5&&month==7&&month==8&&month==10&&month==12)
                                          {if(day>32)
                                              {printf(“号数错误,请检查后重新输入\n”);
                                              goto loop;}
                                              }
                                          
                                          else if(month=2)
                                          {if(day>29)
                                              {printf(“号数错误,请检查后重新输入\n”);
                                              goto loop;}
                                              }
                                          else if(month==4&&month==6&&month==9&&month==11)
                                          {if(day>30)
                                              {printf(“号数错误,请检查后重新输入\n”);
                                              goto loop;}
                                              }
                     }
                     else{
                          printf(“月数输入错误,请检查后重新输入\n”);
                          goto loop;
                          }
                          }
          else
          {
               printf(“年数输入错误,请检查后重新输入\n”);
               goto loop;
               }

  2. wsgc01 说:

    就是把中间的那段循环加进去后就有错误了
    无法算除正确的日期
    哦,这道是题的目的是输入年月日,判断是当年的第几天
    请大哥些帮忙看一下是怎么回事

  3. 5iluo 说:

    越是低级的错误就越难找出来,,

      else if(month=2)

    建议你以后做if这样的判断时把常量放在右边,这样肯定不会错

  4. wsgc01 说:

    非常感谢呀,自己看自己写的真是很难检查,特别是很小的地方
    虽然一般不怎么用GOTO,我还是想用下,呵呵
    什么是吧常量放右边啊

  5. 5iluo 说:

    写成(2 == month)

留下一个回复