java多嵌套结构 判断某年某月有几天

一、题目要求

通过输入年、月,判断某年某月有几天

二、实现代码

/*
date by 2022.07.18
通过输入年、月,判断某年某月有几天
使用多嵌套结构
思路:先输入年份,(1)若输入正确,再输入月份:先判断有30天的月份再判断2月份的天数,此时判断条件为:是否为闰年,闰年的2月份29天;非闰年的2月份28天其余的月份为31天(2)若输入错误,提示输入有误*/import java.util.Scanner;public class Test1 {public static void main(String[] args) {Scanner input = new Scanner(System.in);System.out.print("请输入年份:");//先提示后输入int year = input.nextInt();if (year > 0) {System.out.print("请输入月份:");//先提示后输入int month = input.nextInt();if (month > 0) {if (month == 4 || month == 6 || month == 9 || month == 11) {System.out.println(year + "年" + month + "月有30天");} else if (month == 2) {if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {System.out.println(year + "年" + month + "月有29天");} else {System.out.println(year + "年" + month + "月有28天");}} else {System.out.println(year + "年" + month + "月有31天");}} else {System.out.println("月份输入错误!");}} else {System.out.println("年份输入错误!");}input.close();}
}

三、运行结果

在这里插入图片描述
在这里插入图片描述


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部