根据输入的月份值,判断当月有多少天

练习:根据用户传递的月份值,判断当前月有多少天。如果是2月,根据用户传输的年份,判断2月的天数。switch if两种方法

方法一:public class IfTest{

        public static void main(String args[]){

                int month = Integer.parseInt(args[0]);

                System.out.println("如果输入的是二月份请输入年份 2 2008");

                if(month ==1 || month ==3 || month ==5 || month ==7 || month ==8|| month ==10 || month ==12 ){

                    System.out.println(month +"月有31");

                }else if(month ==1 || month ==6 || month ==9|| month ==11 ){

                    System.out.println(month +"月有30");

                }else if(month == 2){

                      int year = Integer.parseInt(args[1]);

                      if(year%400 == 0) {

System.out.println(year +"" +month +"29");

}else if(year % 4 == 0 && year %100 !=0){

System.out.println(year +""+month+"29");

}

System.out.println(year+""+month+"月有28");

                   }

                System.out.println("end...");

        }

}

方法二:

public class SwitchTest{

        public static void main(String[] args){

                int month = Integer.parseInt(args[0]);

             if(month != 2){

                  switch (month){

                          case 1:

                          System.out.println(month +"31");

                          break;

                          case 3:

                          System.out.println(month +"31");

                          break;

                          case 5:

                          System.out.println(month +"31");

                          break;

                          case 7:

                          System.out.println(month +"31");

                          break;

                          case 8:

                          System.out.println(month +"31");

                          break;

                          case 10:

                          System.out.println(month +"31");

                          break;

                          case 12:

                          System.out.println(month +"31");

                          break;

                          case 4:

                          System.out.println(month +"月有30");

                          break; case 6:

                          System.out.println(month +"月有30");

                          break;

                          case 9:

                          System.out.println(month +"月有30");

                          break;

                          case 11:

                          System.out.println(month +"月有30");

                          break;

                          default:

                          break;

                          }

                     }

                     if(month ==2){

                             System.out.println("如果输入的是2月,请输入如下格式

2 2007");

                             int year = Integer.parseInt(args[1]);

                             if(year % 400 ==0){

                                  System.out.println(month +"29");

                                }else if(year % 4 ==0 && year %100 != 0){

                                  System.out.println(month +"29");

                             }else{

                             System.out.println(year +""+month+"月有28");

                      }

                }

        }

 }

你可能感兴趣的:(根据输入的月份值,判断当月有多少天)