• 编写程序提示用户输入日期,输出这天为星期几。


    package judgeWeekday;
    
    import java.util.Scanner;
    
    public class JudgeWeekday {
    
        //参考点选择2014年1月1日星期三具有一定的特殊性;代码也与之有关。
        public static final int YEAR0 = 2014;
        //public static final int MONTH0 = 1; 
        //public static final int DAY0 = 1;
        public static final int WEEKDAY0 = 3;
        public static final String week[]={"天","一","二","三","四","五","六"};
        public static final int leapmonthday[]={31,29,31,30,31,30,31,31,30,31,30,31};
        public static final int noleapmonthday[]={31,28,31,30,31,30,31,31,30,31,30,31};
        
        
        public static void main(String[] args) {
            
            Scanner console = new Scanner(System.in);
            System.out.print("input the date like 1983 3 29: "); 
            int year = console.nextInt();
            int month = console.nextInt();
            int day = console.nextInt();
            boolean ynleap = judgeleap(year);
            
            int weekday = judgeWeekday (year,month,day,ynleap);
            
            System.out.println( year + "年" + month + "月" + day +"日是星期" + week[weekday]);
        }
        
        
        public static int judgeWeekday (int year,int month,int day,boolean ynleap)
        {
            int sum = computeDaysDistance(year,month,day,ynleap);
            int weekday = getWeekday(sum,year);
            return weekday;
        }
        
        //计算输入输入日期与参考日期相隔的天数。
        public static int computeDaysDistance(int year,int month,int day,boolean ynleap)
        {
            int sum = 0,sum1 = 0,sum2 = 0;
            if(year <  YEAR0)
            {
                //计算sum1
                for(int i = year + 1;i <  YEAR0;i++)
                {   
                    if(judgeleap(i))
                       sum1 += 366;
                    else
                       sum1 += 365;
                }
                //计算sum2
                if(ynleap)
                 {
                   sum2 = leapmonthday[month - 1]- day +1;
                  for(int j = month + 1;j <=12;j++)
                    sum2 += leapmonthday[j - 1];
                 }
                 else
                 {
                   sum2 = noleapmonthday[month -1]- day +1;
                  for(int j = month + 1;j <=12;j++)
                     sum2 += noleapmonthday[j - 1];
                 }
            }
            else
            {
                //计算sum1
                for(int i = YEAR0;i < year;i++)
                 {
                 if(judgeleap(i))
                       sum1 += 366;
                    else
                       sum1 += 365;
                 }
                //计算sum2
                sum2 = day -1;
                for(int j = 1;j <= month -1;j++)
               { 
                    if(ynleap)
                      sum2 += leapmonthday[j - 1];
                    else
                      sum2 += noleapmonthday[j -1];
               }
                
            }
            //计算sum
            sum =sum1 + sum2;
            return sum;
        }
        //给与参考日期相隔的天数得到输入日期是星期几。
        public static int getWeekday(int sum,int year)
        {
            int weekday = 0;
            if(year <  YEAR0)
                 weekday = (WEEKDAY0 + 7 - (sum %7))%7;
            else
                 weekday = (WEEKDAY0 + (sum % 7))%7;
            return weekday;
        }
        //判断给定的year是否闰年。
        public static boolean judgeleap(int year)
        {
             boolean ynleap;
              if(((year % 4 ==0)&&(year % 100!= 0))||(year % 400 == 0))
                 ynleap = true;
              else
                 ynleap = false;
              return ynleap;
        }
    
    }//class
  • 相关阅读:
    js函数柯理化
    Promise异步编程解决方案
    set和map结构,class类
    原创:用node.js搭建本地服务模拟接口访问实现数据模拟
    原创:微信小程序如何使用自定义组件
    原创:微信小程序开发要点总结
    Nodejs CMS——基于 NestJS/NuxtJS 的完整开源项目
    浅谈js对象之数据属性、访问器属性、Object.defineProperty方法
    Promise初步详解(resolve,reject,catch)
    原生js面向对象实现简单轮播
  • 原文地址:https://www.cnblogs.com/diligentcalf/p/3601170.html
Copyright © 2020-2023  润新知