| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2139 人关注过本帖
标题:怎么重复读取数值?
只看楼主 加入收藏
落翼天玑乱
Rank: 1
等 级:新手上路
帖 子:20
专家分:3
注 册:2016-3-7
结帖率:100%
收藏
 问题点数:0 回复次数:9 
怎么重复读取数值?
在编程时候,数值的计算一般都有限制,只能计算某个范围内的数值。比如输入年月日计算星期几,如果以1900年01月01日星期一为基准,就不能计算1900之前的年份,如果用户输入了错误的年份,想要重新读取地话,计算就会出错。如何解决?

scanf(“%d”,&year);
while(year<1900)
{
scanf(“%d”,&year);
}
printf(“%d”,year);
这个程序最后输出的year的值就是最后用户输出的合格的值,但是中间星期的计算就乱了。。求解为什么。。?怎么解决。?
搜索更多相关主题的帖子: 年月日 如何 用户 
2016-03-16 15:29
诸葛欧阳
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:流年
等 级:贵宾
威 望:82
帖 子:2790
专家分:14619
注 册:2014-10-16
收藏
得分:0 
计算星期那部分代码呢

一片落叶掉进了回忆的流年。
2016-03-16 16:14
未来大仙
Rank: 6Rank: 6
来 自:黑窟窿
等 级:侠之大者
威 望:4
帖 子:263
专家分:491
注 册:2015-6-20
收藏
得分:0 
其实我并不明白你问的什么意思。。

好好学习,天天向上!
2016-03-16 17:16
qq1023569223
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:湖南科技大学
等 级:贵宾
威 望:26
帖 子:2753
专家分:13404
注 册:2010-12-22
收藏
得分:0 
计算的部分放在while后面就行了。

   唯实惟新 至诚致志
2016-03-16 21:40
落翼天玑乱
Rank: 1
等 级:新手上路
帖 子:20
专家分:3
注 册:2016-3-7
收藏
得分:0 
回复 2楼 诸葛欧阳
#include <stdio.h>
     int main()
{
   
    printf("------------------------输入年月日,计算星期几----------------------\n\n\n");
   
   

        while(1)
        {
            
            const int biaozhun1=1900;
            const int biaozhun2=1;
            const int biaozhun3=1;
            int year,month,day;
            int y,m,t;
            int days=0,days1=0,days2=0,days3;
            
            printf("请输入年月日(例如2016 3 15):");
            scanf("%d %d %d",&year,&month,&day);
            y=year;        
            
            while(year<1900)
            {
            printf("年份有误,请重新输入:");
            scanf("%d",&year);
            }
            while(month<1||month>12)
            {
                printf("月份有误,请重新输入:");
                scanf("%d",&month);
            }
            while(day<1||day>31)
            {
                printf("日期有误,请重新输入:");
                scanf("%d",&day);
            }
            
        

            printf("计算中,请稍后……") ;
            
            while(y!=biaozhun1)
            {
                if((y-1)%4==0&&(y-1)%100!=0)
                {
                    days1+=366;
                    
                }else
                {
                    days1+=365;
                }
                y--;   
            }
            
            m=month;   
            
            while(m!=biaozhun2)
            {
                if(m==2||m==4||m==6||m==8||m==9||m==11)
                {
                    days2+=31;
                }else if(m==5||m==7||m==10||m==12)
                {
                    days2+=30;
                }else if(m==3)
                {
                    if(year%4==0&&year%100!=0)
                    {
                        days2+=29;
                    }else
                    {
                        days2+=28;
                    }
                }
               
                m--;
            }
            
            days3=day-1;
            
            days=days1+days2+days3;
            
            t=days%7;
            
            switch(t)
            {
                case 0:printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b                                       \n");
                printf("                           %d年%d月%d日是星期一\n\n",year,month,day);break;
                    
                case 1:printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b                                       \n");
                printf("                           %d年%d月%d日是星期二\n\n",year,month,day);break;
                    
                case 2:printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b                                       \n");
                printf("                           %d年%d月%d日是星期三\n\n",year,month,day);break;
                    
                case 3:printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b                                       \n");
                printf("                           %d年%d月%d日是星期四\n\n",year,month,day);break;
                    
                case 4:printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b                                       \n");
                printf("                           %d年%d月%d日是星期五\n\n",year,month,day);break;
                    
                case 5:printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b                                       \n");
                printf("                           %d年%d月%d日是星期六\n\n",year,month,day);break;
                    
                case 6:printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b                                       \n");
                printf("                           %d年%d月%d日是星期日\n\n",year,month,day);break;
                    
                default:printf("运行出错!请调试后运行 !");
            }
        
    }
   
     
   
    system ("pause");
    return 0;
     
 }

运行问题的截图
图片附件: 游客没有浏览图片的权限,请 登录注册


就是说第一次读取错误之后再多次重新读取用户的数据的话就会紊乱,不知道为什么,求解,及其 解决办法!!
2016-03-17 07:35
wp231957
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:神界
等 级:贵宾
威 望:423
帖 子:13688
专家分:53332
注 册:2012-10-18
收藏
得分:0 
清缓冲区

DO IT YOURSELF !
2016-03-17 10:19
落翼天玑乱
Rank: 1
等 级:新手上路
帖 子:20
专家分:3
注 册:2016-3-7
收藏
得分:0 
回复 5楼 落翼天玑乱
  while(year<1900)
            {
            printf("年份有误,请重新输入:");
            fflush(stdin);
            scanf("%d",&year);
            }
            while(month<1||month>12)
            {
                printf("月份有误,请重新输入:");
                fflush(stdin);
                scanf("%d",&month);
            }
            while(day<1||day>31)
            {
                printf("日期有误,请重新输入:");
                fflush(stdin);
                scanf("%d",&day);
            }


是不是这样?我试过了还是不行!
2016-03-17 12:46
落翼天玑乱
Rank: 1
等 级:新手上路
帖 子:20
专家分:3
注 册:2016-3-7
收藏
得分:0 

#include <stdio.h>
     int main()
{
   
    printf("------------------------输入年月日,计算星期几----------------------\n\n\n");
   
   

        while(1)
        {
            
            const int biaozhun1=1900;
            const int biaozhun2=1;
            const int biaozhun3=1;
            int year,month,day;
            int y,m,t;
            int days=0,days1=0,days2=0,days3;
            
            printf("请输入年月日(例如2016 3 15):");
            scanf("%d %d %d",&year,&month,&day);
            
            while(year<1900)
            {
            printf("年份有误,请重新输入:");
            scanf("%d",&year);
            }
            y=year;        //这个赋值原先放错了位置,放在上面了,所以后来重新输入的时候y没有被赋值,所以出现了错误!
            while(month<1||month>12)
            {
                printf("月份有误,请重新输入:");
                scanf("%d",&month);
            }
            while(day<1||day>31)
            {
                printf("日期有误,请重新输入:");
                scanf("%d",&day);
            }
            
        

            printf("计算中,请稍后……") ;
            
            while(y!=biaozhun1)
            {
                if((y-1)%4==0&&(y-1)%100!=0)
                {
                    days1+=366;
                    
                }else
                {
                    days1+=365;
                }
                y--;   
            }
            
            m=month;   
            
            while(m!=biaozhun2)
            {
                if(m==2||m==4||m==6||m==8||m==9||m==11)
                {
                    days2+=31;
                }else if(m==5||m==7||m==10||m==12)
                {
                    days2+=30;
                }else if(m==3)
                {
                    if(year%4==0&&year%100!=0)
                    {
                        days2+=29;
                    }else
                    {
                        days2+=28;
                    }
                }
               
                m--;
            }
            
            days3=day-1;
            
            days=days1+days2+days3;
            
            t=days%7;
            
            switch(t)
            {
                case 0:printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b                                       \n");
                printf("                           %d年%d月%d日是星期一\n\n",year,month,day);break;
                    
                case 1:printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b                                       \n");
                printf("                           %d年%d月%d日是星期二\n\n",year,month,day);break;
                    
                case 2:printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b                                       \n");
                printf("                           %d年%d月%d日是星期三\n\n",year,month,day);break;
                    
                case 3:printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b                                       \n");
                printf("                           %d年%d月%d日是星期四\n\n",year,month,day);break;
                    
                case 4:printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b                                       \n");
                printf("                           %d年%d月%d日是星期五\n\n",year,month,day);break;
                    
                case 5:printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b                                       \n");
                printf("                           %d年%d月%d日是星期六\n\n",year,month,day);break;
                    
                case 6:printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b                                       \n");
                printf("                           %d年%d月%d日是星期日\n\n",year,month,day);break;
                    
                default:printf("运行出错!请调试后运行 !");
            }
        
    }
   
     
   
    system ("pause");
    return 0;
     
 }
代码的问题,已经解决。。O(∩_∩)O谢谢!!!
2016-04-13 19:25
lin5161678
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:45
帖 子:1136
专家分:3729
注 册:2011-12-3
收藏
得分:0 
输入年月日计算星期几

应该用 泰勒公式 之类的工具
不要一天一天的推

https://zh.
2016-04-13 23:33
阳光雨露123
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2016-4-8
收藏
得分:0 
用双重for循环或do-while语句,直到输入的值符合要求才进行下一步
2016-04-14 23:11
快速回复:怎么重复读取数值?
数据加载中...
 
   



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

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