输入某年某月某日,判断这一天是这一年的第几天?考虑闰年的情况
import java.util.Scanner;
/**
*输入某年某月某日,判断这一天是这一年的第几天?考虑闰年的情况
*/
public class Test30 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n, y, r;// 定义年月日变量
System.out.println("please input year:");
n = sc.nextInt();
System.out.println("please input month");
y = sc.nextInt();
System.out.println("please input day");
r = sc.nextInt();
/*
* 限制数据范围,
*/
if (((y == 1) || (y == 3) || (y == 5) || (y == 7) || (y == 8)
|| (y == 10) || (y == 12))
&& r > 31 && r < 0) {
System.out.println("输入数据有误!");
} else if (((y == 4) || (y == 6) || (y == 9) || (y == 11)) && r > 30
&& r < 0) {
System.out.println("输入数据有误!");
} else {
if ((y == 2) && (r > 0 && r < 29)) {
/*
* 开始运算
*/
int sum = 0;// 定义总数
int[] date = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30 };// 定义1-11月的天数
/*
* 求天数的总数
*/
for (int x = 0; x < y; x++) {
sum += date[x];
}
/*
* 判断是不是闰年
*/
if ((n % 400 == 0) || (n % 4 == 0) || (n % 100 == 0)) {
/*
* 这个月份大于2,
*/
if (y > 2) {
System.out.println("you input year is leap year");
System.out.println("you input year: " + n + " month:"
+ y + "day: " + r + "is this year the first: "
+ (sum + r + 1) + "day.");
} else {
System.out.println("you input year: " + n + " month:"
+ y + "day: " + r + "is this year the first: "
+ (sum + r + "day."));
}
} else
System.out.println("you input year: " + n + " month:" + y
+ "day: " + r + "is this year the first: "
+ (sum + r) + "day.");
}
else {
System.out.println("输入数据用误!");
}
}
}
}