java写一个万年历

做一个万年历

代码如下(示例):


import java.lang.invoke.SwitchPoint;
import java.net.StandardSocketOptions;
import java.util.Scanner;

public class Demo1 {

	public static void main(String[] args) {
		System.out.println("==========欢迎使用万年历=========");
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入年份:");
		int year = sc.nextInt();
		System.out.println("请输入月份");
		int month = sc.nextInt();
		System.out.println("请输入一个日期:");
		int d = sc.nextInt();
		int totaldays = 0;//一年的总日数
		String weekday = null;
		
		boolean isRn = false;
		int days = 0;
		for(int i =0;i<year;i++){
			if((year%4==0&&year%100!=0)||(year%400==0)){
				//是润年
				totaldays +=366;
			}else{
				//不是闰年
				totaldays +=365;
			}
		}
		for(int i=0;i<month;i++){
		switch (i) {
		case 1:
		case 3:	
		case 5:	
		case 7:
		case 8:
		case 10:
		//case 12:
			totaldays += 31;
			break;
		case 2:
			if(i%4==0&&i%100!=0||i%400==0){
				totaldays  +=29;
			}else{
				totaldays +=28;
			}
			break;
		case 4:
		case 6:
		case 9:
		case 11:
			totaldays +=30;
			break;
		default:
			System.out.println("输入月份不合法");
			break;
		}
		}
		//计算输入月份第一天
		int week = (days+1)%7;//week代表周几,从周日开始
		System.out.println("日\t一\t二\t三\t四\t五\t六");
		//如果一号在周四  前面打四个空
		for(int i =0;i<week;i++){
			System.out.print("\t");//循环不能换行 不然会数字竖起来排列
		}
		switch (month) {//放的是month,知道打印多少天
		case 1:
		case 3:	
		case 5:	
		case 7:
		case 8:
		case 10:
		case 12:
			for(int i =1;i<32;i++){
				System.out.print(i+"\t");//从一号开始打
				//何时换行 模除7
				if((i+week)%7==0){ 
					System.out.println();
				}
			}
			break;
		case 2:
			int j = year;
			if(j%4==0&&j%100!=0||j%400==0){
				for(int i =1;i<30;i++){
					System.out.print(i+"\t");
					if((i+week)%7==0){ 
						System.out.println();
					}
				}
			}else{
				for(int i =1;i<29;i++){
					System.out.println(i+"\t");
					if((i+week)%7==0){ 
						System.out.println();
					}
				}
				
			}
			break;
		case 4:
		case 6:
		case 9:
		case 11:
			for(int i =1;i<31;i++){
				System.out.print(i+"\t");
				if((i+week)%7==0){ 
					System.out.println();
				}
			}
		}

		
}
}
java编写万年历程序 package Day; import java.util.Scanner; public class Day { static int B1=0; //判断是否为闰年,1代表闰年,0代表平年 static int B2; //判断月份的天数,0代表31天,1代表29天,2代表28天,3代表30天 static int DAY1=0; //计算从1900年到今年1月1日的天数 static int DAY2=0; //计算从今年1月1日到本月1日的天数 static int Month_Day=0; //计算本月的天数 static int Y=1900; //代表起始年,1900年 public static void main(String agrs[]){ System.out.println("请输入年份:"); Scanner sc=new Scanner(System.in); int year = sc.nextInt(); System.out.println("请输入月份:"); int month = sc.nextInt(); //计算从1900年到本年1月1日的天数 for(int i=0;i<=year-1900;i++){ year(Y); if(B1==1&&i!=year-1900){ //如果闰年则一年366天 DAY1=DAY1+366; } else if(B1==0&&i!=year-1900){ //如果是平年,则一年有365天 DAY1=DAY1+365; } Y++; } //计算从本年1月1日到本月1日的天数 for(int j=1;j<=month-1;j++){ //j表示从本年一月份一直到上月,month表示本月 month(j); //month(),表示判断月份性质的方法函数 if(B2==0){ //B2==0,表示本月为31天(1,3,5,7,8,10,12月) DAY2=DAY2+31; } else if(B2==1){ //B2==1,表示本月为闰年的2月,有29天 DAY2=DAY2+29; } else if(B2==2){ //B2==2,表示本月为平年的2月,有28天 DAY2=DAY2+28; } else if(B2==3){ //B2==3,表示本月为其他月份30天(4,6,9,11月) DAY2=DAY2+30; } } month(month); //判断本月共多少天 { if(B2==0){ Month_Day=31; } else if(B2==1){ Month_Day=29; } else if(B2==2){ Month_Day=28; } else if(B2==3){ Month_Day=30; } } // System.out.println(DAY1); // System.out.println("本年已过"+DAY2+"天"); int Today=(DAY1+DAY2)%7+1; //判断本月的第一天是星期几 // System.out.println(Today); // System.out.println(Month_Day); // System.out.println("B1="+B1+" "+"B2="+B2); int num=1; //num表示本月的日期,如:1日,2日,3日。。。。。 System.out.print("一\t"+"二\t"+"三\t"+"四\t"+"五\t"+"六\t"+"七\n"); for(int k=1;num<=Month_Day;k++){ //k代表统计显示日历矩阵的日期的个数,当num小于本月的天数,将继续显示num if(k<Today){ //当本月的第一天所处星期几(即:Today)大于k,则说明前面有Today-k是空的 System.out.print("\t"); //空的需要用制表符占位代替 } else if(k>=Today){ //当k等于Today即刻开始显示日期,从num==1开始显示 System.out.print(num+"\t"); num++; if(k%7==0){ System.out.println(""); //当k是7的整数倍的时候需要换 } } } } public static int year( int year){ if((year%4==0&&year0;!=0)||(year@0==0)){ //System.out.println("这是闰年!"); return B1=1; } else{ //System.out.println("这不是闰年!"); return B1=0; } } public static int month( int month){ if(month==1||month==3||month==5||month==7||month==8||month==10||month==12){ //System.out.println("这个月有31天!"); return B2=0; } else if(month==2&&B1;==1){ //System.out.println("这个月有30天或者28天,或者29天!"); return B2=1; } else if(month==2&&B1;==0){ return B2=2; } else{ return B2=3; } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值