package com.zhp.java;
//用OOP 求出 某年某月某日是当年中的第几天publicclassDay {privateint y;
privateint m;
privateint d;
int[] arr = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
publicDay(int y, int m, int d) {
super();
this.y = y;
this.m = m;
this.d = d;
}
publicDay() {
super();
}
publicintgetY() {
return y;
}
publicvoidsetY(int y) {
this.y = y;
}
publicintgetM() {
return m;
}
publicvoidsetM(int m) {
this.m = m;
}
publicintgetD() {
return d;
}
publicvoidsetD(int d) {
this.d = d;
}
// 判断是否为闰年publicbooleanrunNian() {
if ((y % 4 == 0 && y % 100 != 0) || (y % 400 == 0)) {
returntrue;
} else {
returnfalse;
}
}
// 改变闰年二月的天数publicvoidtwo() {
if (runNian()) {
arr[2] = 29;
}
}
@Overridepublic String toString() {
two();
int sum = 0;
for (int i = 1; i < m; i++) {
sum = sum + arr[i];
}
return y + "年" + m + "月" + d + "日是" + y + "年的" + (sum + d) + "天";
}
}
package com.zhp.java;
import java.util.Scanner;
publicclass DayDemo {
publicstaticvoidmain(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入年份");
int y = sc.nextInt();
System.out.println("请输入月份");
int m = sc.nextInt();
System.out.println("请输入日期");
int d = sc.nextInt();
Day day = new Day(y, m, d);
System.out.println(day.toString());
}
}