java循环小游戏(任意月份日历表)

题目: 输出任意年份任意月份的日历表(公元后)

显示效果:

  1. 思路:
    1. 判断1年1月1日是星期几,1 % 7 = 1 对应的是1年1月1日的星期数,2 % 7 = 2对应的是1年1月2日的星期数,以此类推;
    2. 计算(当年以前所有天数+当年当月1号之前所有天数);
    3. 年份分平年闰年,平年365天,闰年366天;
    4. 闰年的判断方法year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)若为真,则为闰年否则为平年;
    5. 定义平年/闰年数组,包含各月天数;
    6. 遍历数组求和,计算当年当月前总天数;
    7. (当年以前所有天数+当年当月前总天数+1)即为1年1月1日到当年当月1日的总天数;
    8. 总天数对7取模,根据结果判断当月1号是星期几,输出空白区域;
    9. 输出当月日历表,逢星期六换行
  2. 代码1:判断1年1月1日是星期几
/*
题目:已知2016年1月1日是星期五,判断0001年1月1日是星期几
思路:
	1.算出0001年1月1日到2016年1月1日共几天
	2.然后用天数对7取模,判断星期几
*/
class JudgeZero {
	public static void main(String[] args) {
		int totalDay = (((2016 - 1) / 4 * 366) + (2016 - 1 - ((2016-1) / 4)) * 365) + 1;
		int zeroWeek = 1 % 7 - (totalDay % 7 - 5);
		switch (zeroWeek) {
		case 1 :
			System.out.println("0001年1月1日是星期一");
			break;
		case 2 :
			System.out.println("0001年1月1日是星期二");
			break;
		case 3 :
			System.out.println("0001年1月1日是星期三");
			break;
		case 4 :
			System.out.println("0001年1月1日是星期四");
			break;
		case 5 :
			System.out.println("0001年1月1日是星期五");
			break;
		case 6 :
			System.out.println("0001年1月1日是星期六");
			break;
		case 0 :
			System.out.println("0001年1月1日是星期日");
			break;
		default :
			System.out.println("输入错误!");
		}					
	}
}
复制代码
  1. 输出结果:

  2. 代码2:输出日历表

import java.util.Scanner;
class FindMonthList {
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入年份:");
		int year = sc.nextInt();			//年份
		if (year < 1) {						//判断非法输入年份
			System.out.println("输入错误!");
			return;
		}
		System.out.println("请输入月份:");
		int month = sc.nextInt();			//月份
		if (month < 1 || month > 12) {		//判断非法输入月份
			System.out.println("输入错误!");
			return;
		}
		//输出表头
		System.out.println("-------" + year + " 年 " + month + " 月 " + "-------");
		System.out.println();
		System.out.println("日  一  二  三  四  五  六");

		//计算当前年份以前所有天数beforeYearTotalDay;每4年一个闰年,闰年366天,平年365天
		int beforeYearTotalDay = ((year - 1) / 4 * 366) + (year-1 - ((year - 1) / 4)) * 365;
		int[] arrLeapYear = {0,31,29,31,30,31,30,31,31,30,31,30,31};	//闰年各月天数	int数组
		int[] arrNormalYear = {0,31,28,31,30,31,30,31,31,30,31,30,31};	//平年各月天数	int数组
		int beforeMonthTotalDay = 0;									//定义本年当月之前月份的总天数
		if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)) {		//判断当前年份是否是闰年
			for (int i = 0 ; i < month ; i ++ ) {	//for循环计算当月之前总天数
				//计算当前月份之前的所有天数
				beforeMonthTotalDay = beforeMonthTotalDay + arrLeapYear[i];
			}
			//判断当月1日是星期几
			int totalDay = beforeYearTotalDay + beforeMonthTotalDay + 1;
			int week = totalDay % 7;//已知1年1月1日是星期日,即模7得1对应的是星期日
			for (int i = 0 ; i < (week - 1 + 7) % 7 ; i ++) {	//如果写成i < (week-1)会出现i<-1的情况
				System.out.print("    ");//输出开头空白
			}
			for (int i = 1 ;i <= arrLeapYear[month] ;i ++ ) {	//for循环输出各月天数
				System.out.print(i + "  ");
				if (i < 10 ) {		//小于10的数补一个空格,以便打印整齐
					System.out.print(" ");
				}
				if (i % 7 == ((7-(week - 1)) % 7 ) || i == arrLeapYear[month]) {//每逢星期六/尾数换行
					System.out.println();
				}
			}

		} else {		//不是闰年就是平年
			for (int i = 0 ; i < month ; i ++ ) {	//for循环计算出当月之前月份总天数
				beforeMonthTotalDay = beforeMonthTotalDay + arrNormalYear[i];
			}
			//判断当月1日是星期几
			int totalDay = beforeYearTotalDay + beforeMonthTotalDay + 1;
			int week = totalDay % 7;//已知1年1月1日是星期日,即模7得1对应的是星期日
			for (int i = 0 ; i < (week - 1 + 7) % 7 ; i ++) {	//如果写成i < (week-1)会出现i<-1的情况
				System.out.print("    ");//输出开头空白
			}
			for (int i = 1 ;i <= arrNormalYear[month] ;i ++ ) {//for循环输出各月天数
				System.out.print(i + "  ");
				if (i < 10 ) {			//小于10的数补一个空格,以便打印整齐
					System.out.print(" ");
				}
				if (i % 7 == ((7-(week - 1)) % 7 ) || i == arrNormalYear[month]) {//每逢星期六/尾数换行
					System.out.println();
				}
			}
		}
	}
}
复制代码
  1. 输出结果:
  2. 总结:
  • 代码还很臃肿,需要继续优化;
  • 对于数组的理解还很浅显,下一步学习数组定义,用一个数组解决平闰年的问题;
  • 类名、变量名命名不规范,意思不易理解。

转载于:https://juejin.im/post/5a34e6c06fb9a0450f22024e

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好,以下是一个示例代码,可以循环输出每个月的日历表: ```java import java.time.LocalDate; import java.time.format.DateTimeFormatter; public class CalendarPrinter { public static void main(String[] args) { int year = 2022; // 年份 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); for (int month = 1; month <= 12; month++) { // 循环输出每个月的日历表 LocalDate date = LocalDate.of(year, month, 1); int daysInMonth = date.lengthOfMonth(); // 获取本月天数 int firstDayOfWeek = date.getDayOfWeek().getValue(); // 获取本月第一天是星期几 String[][] calendar = new String[6][7]; // 存储本月日历表 int dayOfMonth = 1; for (int i = 0; i < 6; i++) { for (int j = 0; j < 7; j++) { if (i == 0 && j < firstDayOfWeek - 1) { calendar[i][j] = " "; // 填充空白 } else if (dayOfMonth > daysInMonth) { calendar[i][j] = " "; // 填充空白 } else { calendar[i][j] = String.format("%2d", dayOfMonth++); // 填充日期 } } } System.out.println(date.getMonth().toString() + " " + year); // 输出月份和年份 System.out.println("Mo Tu We Th Fr Sa Su"); // 输出星期标题 for (String[] row : calendar) { for (String day : row) { System.out.print(day + " "); } System.out.println(); } System.out.println(); } } } ``` 这段代码使用 Java 8 的日期时间 API,计算出每个月的天数和第一天是星期几,然后生成一个二维数组存储日历表,最后循环输出日历表。运行代码后,会输出 2022 年每个月的日历表
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值