题目:   编写一个程序,接受用户输入的1~12之间的整数,若不符合则重输入,,利用swich语句输出对应月份的天数.弄了一下午,现在可以运行,但是当输入的整不符合要求时,按说应该继续输入才对啊,但是如果输入不符合条件时就停止了,怎么回事,谁告诉下,现在不知道心情怎么样,这个程序应该是很简单的了,我竟然弄了一下午,而且现在还有问题
import java.util.*;
public class test1 {    /**
    * @param args
    */
    public static void main(String[] args) {
     int month;
        System.out.println("Input the month");
     do{
            Scanner sc = new Scanner(System.in);
            month = sc.nextInt();
            if(month < 1||month > 12)
                System.out.println("Error,Input again");
     }while(month > 12&&month < 1);// TODO Auto-generated method stub
        switch (month){
        case 1:System.out.println("31");break;
        case 3:System.out.println("31");break;
        case 5:System.out.println("31");break;
        case 7:System.out.println("31");break;
        case 8:System.out.println("31");break;
        case 10:System.out.println("31");break;
        case 12:System.out.println("31");break;
        case 2:System.out.println("28");break;
        case 4:System.out.println("30");break;
        case 6:System.out.println("30");break;
        case 9:System.out.println("30");break;
        case 11:System.out.println("30");break;
        }
    }
}

解决方案 »

  1.   

    while(month > 12 && month < 1);
    什么数能够即大于12又小于1呢?
      

  2.   


     public static void main(String[] args) { 
            int month=0; 
               System.out.println("Input the month"); 
            do{ 
                   Scanner sc = new Scanner(System.in); 
                   String s = sc.nextLine();//这里你要读整行!不是读一位!
                   try {
                   month = Integer.valueOf(s);
                   }catch(Exception e) {
                       System.out.println("输入错误!请重新输入!");
                       System.exit(1);
                   }
                   if(month  < 1 ||month > 12) 
                       System.out.println("Error,Input again"); 
            }while(month > 12||month  < 1);
               switch (month){ 
               case 1:System.out.println("31");break; 
               case 3:System.out.println("31");break; 
               case 5:System.out.println("31");break; 
               case 7:System.out.println("31");break; 
               case 8:System.out.println("31");break; 
               case 10:System.out.println("31");break; 
               case 12:System.out.println("31");break; 
               case 2:System.out.println("28");break; 
               case 4:System.out.println("30");break; 
               case 6:System.out.println("30");break; 
               case 9:System.out.println("30");break; 
               case 11:System.out.println("30");break; 
               } 
           } 
      

  3.   

    我理解錯了nextInt,主要是邏輯判斷!if(month  < 1 ||month > 12) 
      

  4.   

    mport java.util.*; 
    public class month {     /** 
        * @param args 
        */ 
        public static void main(String[] args) { 
         int month=0; 
            System.out.println("Input the month"); 
            
           
           
         do{ 
          Scanner sc =new Scanner (System.in);
          month = Integer.parseInt(sc.nextLine());
        
        
                if(month>12 || month<1)
                 {
                 System.out.println("Error,Input again"); 
                
                 }
         }while(month >12|| month  <1);
          
         
            switch (month)
            {
            case 1:System.out.println("31");break; 
            case 3:System.out.println("31");break; 
            case 5:System.out.println("31");break; 
            case 7:System.out.println("31");break; 
            case 8:System.out.println("31");break; 
            case 10:System.out.println("31");break; 
            case 12:System.out.println("31");break; 
            case 2:System.out.println("28");break; 
            case 4:System.out.println("30");break; 
            case 6:System.out.println("30");break; 
            case 9:System.out.println("30");break; 
            case 11:System.out.println("30");break; 
            } 
        } 
    }
      

  5.   

    while这里改为:while(month >12 ¦ ¦ month   <1); 
      

  6.   

    import java.util.*; 
    public class test1 {     /** 
        * @param args 
        */ 
        public static void main(String[] args) { 
         int month; 
            System.out.println("Input the month"); 
         do{ 
                Scanner sc = new Scanner(System.in); 
                month = sc.nextInt(); 
                if(month  < 1 ¦ ¦month > 12) 
                    System.out.println("Error,Input again"); 
         }while(month < 12&&month  > 1);// TODO Auto-generated method stub   //应该是在1~12之间
            switch (month){ 
            case 1:System.out.println("31");break; 
            case 3:System.out.println("31");break; 
            case 5:System.out.println("31");break; 
            case 7:System.out.println("31");break; 
            case 8:System.out.println("31");break; 
            case 10:System.out.println("31");break; 
            case 12:System.out.println("31");break; 
            case 2:System.out.println("28");break; 
            case 4:System.out.println("30");break; 
            case 6:System.out.println("30");break; 
            case 9:System.out.println("30");break; 
            case 11:System.out.println("30");break; 
            } 
        } 
    }