编程语言
首页 > 编程语言> > java写一个万年历

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();
				}
			}
		}

		
}
}

标签:case,totaldays,java,万年历,一个,System,int,println,out
来源: https://blog.csdn.net/dgk870110/article/details/115596665