Java语言写出"输入年份(year)和月份(month),首先判断year是否为闰年:如果是闰年,那么对应2月份有29天;如果不是闰年,那么对应2月份有28天。然后,根据输入的月份,判断该月有多少天数,并输出结果。算法结束。"的程序代码
时间: 2024-06-04 18:13:53 浏览: 69
C#、vb.net及SQL判断指定年份是否为闰年的方法
5星 · 资源好评率100%
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int year, month, days;
System.out.print("请输入年份:");
year = sc.nextInt();
System.out.print("请输入月份:");
month = sc.nextInt();
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
if (month == 2) {
days = 29;
} else if (month == 4 || month == 6 || month == 9 || month == 11) {
days = 30;
} else {
days = 31;
}
} else {
if (month == 2) {
days = 28;
} else if (month == 4 || month == 6 || month == 9 || month == 11) {
days = 30;
} else {
days = 31;
}
}
System.out.println(year + "年" + month + "月份有" + days + "天");
}
}
阅读全文