java 平年闰年的判断,并输出月份天数

作者:分开不是尽头 | 创建时间: 2023-06-20
编程题:给定年份和月份,判断该月下有多少天。 说明: 1月份:31 2月份:平年28,闰年29 3月份:31 4月份:30 5月份:31 6月份:30 7月份:31 8月份:31 9月份:30 10月份:31 11月份:30 12月份:31...
java 平年闰年的判断,并输出月份天数

操作方法

package zixi_3; public class l2 { public static void main(String[] args) { //int i=2000; //if(i%4==0&&i%100==0||i%400==0){System.out.println(i+"是闰年");} //else{System.out.println(i+"是平年");} int i=2001; String str=(i%4==0&&i%100==0||i%400==0)?"闰年":"平年"; System.out.println(i+str); } }

//int i=2000; //if(i%4==0&&i%100==0||i%400==0){System.out.println(i+"是闰年");} //else{System.out.println(i+"是平年");} 这是第二种方法,判断。

public class lian3 { public static void main(String[] args) { int year = 2017; int month = 7; if (year % 4 == 0 && year % 100 == 0 || year % 400 == 0) {

System.out.println(year + "是闰年"); } else { System.out.println(year + "是平年"); } if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) { System.out.println(month + "月份有31天。"); } else if (month == 4 || month == 6 || month == 9 || month == 11) {

System.out.println(month + "月份有30天。"); } else if (month == 2) { if (year % 4 == 0 && year % 100 == 0 || year % 400 == 0) { System.out.println(month + "月份有29天。"); }else {System.out.println(month + "月份有28天。");}}}}

给定年份和月份,判断该月下有多少天。 首先判断平年闰年,输出。然后把月份分成三部分,1,3,5,7,8,10,12为31天,2月份两种情况,嵌套if语句。即可

温馨提示

使用英文输入法
点击展开全文

更多推荐