一个判断某年某月天数的方法…
package com.chixing.day02;
import java.util.Scanner;
/*
手动输入年,月,输出当前月的天数
*/
public class IfElseDemo_2 {
public static void main(String[] args) {
//输入年份
System.out.print("请输入年份:");
int year = new Scanner(System.in).nextInt();
System.out.print("请输入月份[1-12]:");
int month = new Scanner(System.in).nextInt();
//向方法中传递参数,年份和月份
IfElseDemo_2.printDay(year,month);
}
//方法,输出当前月的天数,从键盘获取数据
//static 实现本类类名调用
public static void printDay(int year,int month){
//对获取得年份和月份进行判断
//判断年份,不能低于计算机诞生的元年
if(year < 1972 )
System.out.println("计算机都没出生,我不算了");
//判断月份是否在1-12之间
else if(month < 1 || month > 12 )
System.out.println("一年有那几个月啊??");
//只要是4,6,9,11月,固定都是30天
else 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("因为今年是闰年,所以有29天");
else
System.out.println("因为今年不是闰年,所以只有28天");
}else
//剩下的月份都是固定每月31天
System.out.println(year+"年"+month+"月有31天");
}
}