方法区
package com.calendar;
public class Calendar {
int year;//定义年份
int month;//定义月份
/*求年份总天数(1990-输入年数)*/
int yearSum = 0;
public int getyearSum() {
for(int i=1900;i<year;i++) {
if((i%100!=0&&i%4==0)||(i%100==0&&i%400==0)) {
yearSum+=366;
}else {
yearSum+=365;
}
}
return yearSum;
}
/*求月份总天数*/
int monthSum = 0;
public int getmonthSum() {
for(int i=1;i<month;i++) {
monthSum+=getday(year,i);
}
return monthSum;
}
/*求每月第一天是星期几*/
int week = 0;
public int getweek() {
week = (yearSum+monthSum+1)%7;
return week;
}
/*求每月有多少天*/
int day = 0;
public int getday(int year,int month) {
switch(month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
day =31;
return day;
case 2:
if(isleapYear()==true) {
day= 29;
return day;
}else {
day= 28;
return day;
}
default:
day= 30;
return day;
}
}
/*判断是否是闰年*/
boolean leapYear;
public boolean isleapYear() {
if((year%100!=0&&year%4==0)||(year%100==0&&year%400==0)) {
leapYear = true;
}else {
leapYear = false;
}
return leapYear;
}
}
调用区
package com.calendar;
import java.util.Scanner;
public class CalendarDemo {
public static void main(String[] args) {
Calendar c = new Calendar();
Scanner sc = new Scanner(System.in);
System.out.println("请输入年份");
c.year= sc.nextInt();
System.out.println("请输入月份");
c.month = sc.nextInt();
c.getyearSum();
c.getmonthSum();
c.getweek();
c.getday(c.year,c.month);
c.isleapYear();
System.out.println(c.yearSum);
System.out.println(c.monthSum);
System.out.println(c.day);
System.out.println(c.year+"年"+c.month+"月");
System.out.println("星期日\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六");
int count = 0;
for(int i=1;i<=c.week;i++) {
System.out.print("\t");
count++;
}
for(int a=1;a<=c.day;a++) {
if((a+count)%7==0) {
System.out.print(a+"\t");
System.out.println();
}else {
System.out.print(a+"\t");
}
}
}
}