题目:键盘输入年份和月份,然后控制台返回该月份的天数(闰年2月29天).
(闰年:年份是整百数时,必须是400的倍数才是闰年)
- import java.util.Scanner;
- public class Mymonth {
- 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();
-
- if(year%400 == 0){
- System.out.println("您输入的年份是闰年!");
- switch(month){
- case 1:
- case 3:
- case 5:
- case 7:
- case 8:
- case 10:
- case 12:
- System.out.println("您输入的月份有31天!");
- break;
- case 4:
- case 6:
- case 9:
- case 11:
- System.out.println("您输入的月份有30天");
- break;
- case 2:
- System.out.println("您输入的月份有29天!");
- break;
- default:
- System.out.println("您输入的月份有误!");
- break;
- }
-
- }else{
- System.out.println("您输入的年份不是闰年!");
- switch(month){
- case 1:
- case 3:
- case 5:
- case 7:
- case 8:
- case 10:
- case 12:
- System.out.println("您输入的月份有31天!");
- break;
- case 4:
- case 6:
- case 9:
- case 11:
- System.out.println("您输入的月份有30天");
- break;
- case 2:
- System.out.println("您输入的月份有28天!");
- break;
- default:
- System.out.println("您输入的月份有误!");
- break;
- }
- }
-
- }
- }
复制代码
感觉自己写的太麻烦了,有没有简单一点的方法? |
|