一、需求:
已知1900年1月1日是星期一,用户输入年份和月份,可以打印出该月的日历
二、思路:
1、确定该月的总天数
2、需要确定该月的1号是星期几(可以通过计算该月1号距离1900年1月1号的天数,然后将天数对7取模,结果是几,就说明是星期几)
3、打印格式:
- 需要确定1号的位置(1号前面需要空几格)
- 什么时候换行(如果是星期六就要换行,也就是天数模7的结果是6的时候需要换行)
三、代码:
package com.calender.test;
import java.util.Scanner;
public class Calender{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("请输入年份");
int year=sc.nextInt();
System.out.println("请输入月份");
int month=sc.nextInt();
/*
1、计算当月总天数
*/
int days;
if(month==2){
if(year%4==0&&ye