日历(Java实现)

时隔两年再写博文(开的小号为了重新展示我菜来实现让别人意识到有人比他们菜的人生价值)

在c++全还回去的情形下初学Java
贴个代码先?(Markdown要一点也不会用了
写了个打印日历
迈出的第一步,纪念一下

package a;
//------------------打印日历
import java.util.Scanner;
public class Calendar1{
	public static int which_day(int x){
		//--------------------------------------------------------------------------------------------------返回x年1月1号周几
		int y=x,days=0,flag;
		if(x<2019) {
			y=2019;
			flag=-1;
		}
		else {
			x=2019;
			flag=1;
		}
		for(int i=x;i<y;i++){
			if(isrun(i)) days+=366;
			else days+=365;
		}
		return ((2+days*flag)%7+7)%7;
	}
	public static boolean isrun(int x){
		if(x%100 == 0){
			if(x%400 == 0) return true;
			else return false;
		}
		else{	
			if(x%4 == 0) return true;
			else return false;
		}
	}
	public static void print_title(int k){
		if(k==0) System.out.println("         Jan                     Feb                     Mar         ");
		if(k==1) System.out.println("         Apr                     May                     Jun         ");
		if(k==2) System.out.println("         Jul                     Aug                     Sep         ");
		if(k==3) System.out.println("         Oct                     Nov                     Dec         ");//---------------输出各月表头
		System.out.println("日  一  二  三  四  五  六        日  一  二  三  四  五  六        日  一  二  三  四  五  六     ");
	}
	public static void main(String[] args){

		Scanner scan=new Scanner(System.in);
		int year,date[]=new int[3],day[]=new int[3];
		int month[][]=new int[4][4];
		
		System.out.print("                                                   年份:");
		year=scan.nextInt();
		System.out.println();
		
		month[0][0]=month[0][2]=month[1][1]=month[2][0]=month[2][1]=month[3][0]=month[3][2]=31;
		month[0][1]=isrun(year)?29:28;
		month[1][0]=month[1][2]=month[2][2]=month[3][1]=30;//-----------------------------------------------------打表各月天数
				
		day[2]=(((which_day(year)-1)%7)+7)%7;//------day[2]是为了循环里给day[0]赋值

		for(int k=0;k<4;k++){
            print_title(k);
			date[0]=date[1]=date[2]=1;
			day[0]=(day[2]+1)%7;
			day[1]=(day[0]+month[k][0])%7;
			day[2]=(day[1]+month[k][1])%7;
			
			while(date[0] <= month[k][0] || date[1] <= month[k][1] || date[2]<= month[k][2]){
				for(int i=0;i<3;i++){
					if(date[i]<=month[k][i]){
						if(date[i]==1){
							for(int j=0;j<day[i];j++) 
								System.out.print("   ");//月头空格
						}
						while(date[i]<=month[k][i] && day[i]<=6){
							if(date[i]<10) System.out.print(" ");
							System.out.print(date[i]+" ");
							date[i]++;
							day[i]++;//-------------------打印当前月当前行
						}
						if(date[i]==month[k][i]+1){
							for(int j=day[i];j<7;j++) 
								System.out.print("   ");//月尾空格
						}
						if(day[i]==7 || date[i]==month[k][i]+1) System.out.print("   ");	
					}
					else{
						for(int j=0;j<=7;j++)
							System.out.print("   ");//处理前面的月份打印完,但是后面月份未打印完的情况
					}
				}
				System.out.println("\n");
				day[2]--;
				for(int j=0;j<3;j++) day[j]=(day[j]+7)%7;
			}
		}
	}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值