public class Calendar{
static int month = 0;
static int year = 0;
public static void main(String[] args) {
System.out.println("input year ");
Scanner y= new Scanner(System.in);
year = y.nextInt();
System.out.println("input month");
Scanner m= new Scanner(System.in);
month = m.nextInt();
//month = Integer.parseInt(args[0]);//得到用户输入的月份
if (year % 400 == 0){
System.out.println(year+"年"+"2月有29天");
}
else if (year % 4 == 0)
{if (year % 100 != 0){if (month==2){
System.out.println(year+"年"+"2月有29天");
}}
}
else{
Calendar c = new Calendar();
c.CalulateDay();
}
}
static int CalulateDay() {
if (month == 2) //使用IF分支控制判断月份拥有的天数
{
System.out.print(month + "月有28天");
} else if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
System.out.print(year+"年"+month + "月有31天");
} else {
System.out.print(year+"年"+month + "月有30天");
}
return month;
}
}
/*你这边的代码我做了一下简化,我知道你的意思,就是判断某年某月的天数,分闰年情况,可以把判断都放在你设的方法里,*
*还有你没传参数,所以你这边应该会总是显示2月的天数对吧,答题不易,还望**博友采纳**,可直接运行我的代码的
*代码如下:
*/
static int month = 0;
static int year = 0;
public static void main(String[] args) {
System.out.println("input year ");
Scanner y= new Scanner(System.in);
year = y.nextInt();
System.out.println("input month");
Scanner m= new Scanner(System.in);
month = m.nextInt();
CalulateDay(year,month);
}
static int CalulateDay(int year,int month) {
if (month == 2) //使用IF分支控制判断月份拥有的天数
{
if(year%400==0||(year%4==0&&year%100!=0)){
System.out.print(month + "月有29天");
}
else{
System.out.print(month + "月有28天");
}
} else if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
System.out.print(year+"年"+month + "月有31天");
} else {
System.out.print(year+"年"+month + "月有30天");
}
return month;
}