zoukankan      html  css  js  c++  java
  • java实现任意年份的日历打印

    初学,示例代码里看到的觉得很强大,自己也试试。

    package du;
    
    import java.util.Scanner;
    public class Test {
        public static void main(String argv[]){
            while(true){
            System.out.println("输入年份:");
            Scanner sc=new Scanner(System.in);
            int year=sc.nextInt();
            System.out.println(year);
            System.out.println();
            for(int i=1;i<13;i++){
                System.out.println(i+"月");
                System.out.println("----------------------------------");
                System.out.println("星期日    星期一    星期二   星期三   星期四    星期五    星期六");
                ShowMonth(year,i);
            }
            }
        }
        public static void ShowMonth(int year,int month){
            int firstday=weekday(year, month, 1);
            for(int i=0;i<firstday;i++){
                System.out.print("     ");
            }
            for(int j=1;j<=daysofMonth(year, month);j++){
                if(j<10){
                System.out.print("0"+j+"   ");
                }
                else
                    System.out.print(j+"   ");
                
                if(firstday==6){
                    System.out.println();
                    firstday=-1;
                }
                firstday++;
            }
            System.out.println();
            System.out.println("----------------------------------");
        }
        public static boolean isLeapYear(int year){
            boolean flag = false;
    
            if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
                flag = true;
            else
                flag = false;
            return flag;
        }
        
        public static int weekday(int year,int month,int day){
            long amount=0;
            for(int i=1;i<year;i++){
                if(isLeapYear(i))
                    amount++;
            }
            amount+=365*(year-1);
            for(int i=1;i<month;i++){
                amount+=daysofMonth(year, i);
            }
            amount+=day;
            //System.out.println(amount);
            int week=(int) (amount%7);
            return week;    
        }
        private static int daysofMonth(int year,int month){
            
            switch(month){
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:return 31;
            case 4:
            case 6:
            case 9:
            case 11:return 30;
            case 2: if(isLeapYear(year))
                return 29;
            else
                return 28;
            default :return 0;
            }
            
        }
        
    }
  • 相关阅读:
    第七周-学习进度条
    《梦断代码》阅读笔记01
    第六周-学习进度条
    构建之法阅读笔记03
    结对项目开发(石家庄地铁乘车系统)
    第五周-学习进度条
    第四周-学习进度条
    HDU--1272 小希的迷宫(并查集判环与联通)
    HDU--1856 More is better(简单带权并查集)
    HDU--3635 带权并查集
  • 原文地址:https://www.cnblogs.com/BeyondTime/p/2680153.html
Copyright © 2011-2022 走看看