首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >【JAVA 课程设计 之 万年历】「建议收藏」

【JAVA 课程设计 之 万年历】「建议收藏」

作者头像
全栈程序员站长
发布2022-07-25 21:24:15
发布2022-07-25 21:24:15
6760
举报

大家好,又见面了,我是你们的朋友全栈君。

万年历:输入要查看的年份,输出该年份的日历

实现代码:

代码语言:javascript
复制
package CJF;

import java.util.*;

public class ks {

	public static void main(String[] args) {
          int x = 3,n,m,y,r,k;
          System.out.println("******您好,2016年华人款万年历,枫枫为您服务******");
          System.out.println("**********************请输入查找年份********");
          System.out.println();
          Scanner input = new Scanner(System.in);
          n = input.nextInt(); 
          int cut = 365 * 3 + 366;
          if(n >= 1997){
        	  m = n - 1997;
        	  int a = m / 4;
        	  int b = m % 4;
        	  x = (x + cut * a + b * 365) % 7;
          }
          else{
        	  m = 1996 - n;
        	  x =(366 - x + 2) % 7; // 先转换为1996年为基准年份
        	  int a = m / 4;
        	  int b = m % 4;
        	  cut = (cut * a + b * 365) % 7;
        	  x = (x - cut + 7) % 7; 

          }
          if(x == 0) x = 7;
          for(y = 1 ; y <= 12 ; y++){
            	  if(y == 1 || y == 3 || y == 5 || y == 7 || y == 8 || y == 10 || y == 12)
            		  k = 31;
            	  else if(y == 2){
            		  if(n % 400 == 0 || (n % 4 == 0 && n % 100 != 0))
            			  k = 29;
            		  else 
            			  k = 28;
            	  }
            	  else
            		  k = 30;
            	  System.out.println();
            	  System.out.println("********** " + n + " 年  " + y + " 月  " + "**********");
            	  System.out.println("Mon  Tue  Wen  Thu  Fri  Sat  Sun");
            	  for(int i = 1 ; i < x ; i++)
            		  System.out.print("     ");
            	  for(r = 1; r <= k; r++,x++){
            		  if(r <= 9)
            		      System.out.print(r + "    ");
            		  else
            			  System.out.print(r + "   ");
            		  if(x % 7 == 0){
            			  System.out.println();
            		      x = 0;
            		  }
            	  }
            	  System.out.println();
              }
          System.out.println("******本次服务到处结束,枫枫期待与您的下次会面******");
          }
	}

对JAVA来讲一个main方法写下了总感觉不太合适,把代码格式稍微改下层次和结构更清晰了点~

实现代码:

代码语言:javascript
复制
package CJF;

import java.util.*;

public class ks {

    public static void main(String[] args) {
          System.out.println("******您好,2016年华人款万年历,枫枫为您服务******");
          System.out.println("**********************请输入查找年份********");
          System.out.println();
          Scanner input = new Scanner(System.in);
          int n = input.nextInt(); 
          int x = Nf(n); // 确认该年第一天是星期几
          if(x == 0) x = 7;
          for(int y = 1 ; y <= 12 ; y++){
              int k = Yf(n,y); // 判断该年该月有多少天
                  System.out.println();
                  System.out.println("********** " + n + " 年  " + y + " 月  " + "**********");
                  System.out.println("Mon  Tue  Wen  Thu  Fri  Sat  Sun");
                  x = R(x,k); // 调整日的输出格式
              }
          System.out.println("******本次服务到处结束,枫枫期待与您的下次会面******");
    }
    public static int Nf(int n){ // 确认该年第一天是星期几
        int m,x = 3;
        int cut = 365 * 3 + 366;
        if(n >= 1997){
            m = n - 1997;
            int a = m / 4;
            int b = m % 4;
            x = (x + cut * a + b * 365) % 7;
        }
        else{
            m = 1996 - n;
            x =(366 - x + 2) % 7; // 先转换为1996年为基准年份
            int a = m / 4;
            int b = m % 4;
            cut = (cut * a + b * 365) % 7;
            x = (x - cut + 7) % 7; 
        }
        return x;
    }
    public static int Yf(int n,int y){ // 判断该年该月有多少天
        int k = 0;
            if(y == 1 || y == 3 || y == 5 || y == 7 || y == 8 || y == 10 || y == 12)
                k = 31;
            else if(y == 2){
                if(n % 400 == 0 || (n % 4 == 0 && n % 100 != 0))
                    k = 29;
                else 
                    k = 28;
            }
            else
                k = 30;
        return k;
    }
    public static int R(int x,int k){ // 调整日的输出格式
        for(int i = 1 ; i < x ; i++)
            System.out.print("     ");
        for(int r = 1; r <= k; r++,x++){
            if(r <= 9)
                System.out.print(r + "    ");
            else
                System.out.print(r + "   ");
            if(x % 7 == 0){
                System.out.println();
                x = 0;
            }
        }
        System.out.println();
        if(x == 0) x = 7;
        return x;
    }
}

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/127925.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年4月1,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档