在控制台输入一个日期(分别输入年、月、日),判断输入的日期是否有效,如果有效,计算该日期的下一天日期,并显示;否则,输出“无效的日期”后退出
package javaTest2.T1;
import java.util.Scanner;
public class date3 {
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();
System.out.println("输入一个日期:日");
int day = sc.nextInt();
int y = year;
int m = month;
int d = day;
int i = 0;
if (year <= 0 || month <= 0 || day <= 0)
i++;
switch (month) {
case 1 :
case 3 :
case 5 :
case 7 :
case 8 :
case 10 :{
if (day < 31)
d = day + 1;
else if (day == 31) {
m = month + 1;
d = 1;
}
else
i++;
break;
}
case 12 :{
if (day < 31)
d = day + 1;
else if (day == 31) {
y = year + 1;
m = 1;
d = 1;
}
else
i++;
break;
}
case 4:
case 6:
case 9:
case 11:{
if (day < 30)
d = day + 1;
else if (day == 30) {
m = month + 1;
d = 1;
}
else
i++;
break;
}
case 2:{
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
if (day < 29)
d = day + 1;
else if (day == 29) {
m = 3;
d = 1;
}
else
i++;
break;
}
else {
if (day < 28)
d = day + 1;
else if (day == 28) {
m = 3;
d = 1;
}
else
i++;
break;
}
}
}
if (i != 0) {
System.out.println("无效的日期");
}
else {
System.out.println("下一日日期:"+y+"."+m+"."+d);
}
}
}