编写一个Java程序,接受用户输入的一个1~12之间的整数(如果输入的数据不满足这个条件,则要求用户重新输入),利用switch语句输出对应月份的天数
请高手给个代码

解决方案 »

  1.   


    import   java.io.*; public   class   INT   { public   static   void   main(String[]   args)   { 
    BufferedReader   br=new   BufferedReader(new   InputStreamReader(System.in));   
    System.out.println( "please   input   int(1,2,3,4,5,6,7,8,9,10,11,12)  to  choose! ");   try   { 
    int   num   =  Integer.parseInt(br.readLine()); 
    switch(num)   
    {   
    case 1:   
    System.out.println( "you   choose   1 ");   
    break;   
    case   2:   
    System.out.println( "you   choose   2 ");   
    break;   
    case   3:   
    System.out.println( "you   choose   3 ");   
    break;  
    case  4:   
    System.out.println( "you   choose   4 ");   
    break;
    case   5 :  
    System.out.println( "you   choose   5 ");   
    break;
    case   6:   
    System.out.println( "you   choose   6 ");   
    break;
    case   7:   
    System.out.println( "you   choose   7 ");   
    break;
    case   8:   
    System.out.println( "you   choose   8");   
    break;
    case   9:   
    System.out.println( "you   choose   9 ");   
    break;
    case   10:   
    System.out.println( "you   choose   10 ");   
    break; 
    case   11:   
    System.out.println( "you   choose   11 ");   
    break;
    case   12  :
    System.out.println( "you   choose   12 ");   
    break;default:   
    System.out.println( "you   choose   errror ");   
    break;   

    }   catch   (Exception   e)   { e.printStackTrace(); 
    }   } 

      

  2.   

    switch 已经有朋友写了,我写个不是switch的吧
    不过由于不知道年份,按理说是不知道天数的因为有闰年的说法import java.util.Scanner;public class TestMonth {
    public static void main(String[] args){
    TestMonth testMonth = new TestMonth();
    testMonth.printHowmanyDay();
    }
       public static int[] DAYS = {31,28,31,30,31,30,31,31,30,31,30,31};
       public void printHowmanyDay(){
       Scanner in = new Scanner(System.in);
           try{
            int i = in.nextInt();
            showDay(i);
           }
           catch(Exception nfex){
            System.out.println("数字格式错误");
           }
       }
       public void showDay(int month){
       System.out.println(month + "月一共有" + DAYS[month-1] + "天");
       }
    }