怎么可以在控制台只输入int 型 ,而输入其他类型时提示禁止。      新手上路
   谢谢提携

解决方案 »

  1.   

    public class input {  public static void main(String[] args) {
        String aa = args[0];
        if (intflg(aa)) {
          System.out.println("Integer type!");
        }
        else{
          System.out.println("not Integer type!");
        }
      }  static boolean intflg(String str) {
        int index;
        String intArray = "0123456789";
        if(str==null || str.equals("")){
          return false;
        }
        for (int i = 0; i < str.length(); i++) {
          index = intArray.indexOf(str.charAt(i));
          if (index == -1) {
            return false;
          }
        }
        return true;
      }
    }
      

  2.   

    public class input {   public static void main(String[] args) { 
        String aa = args[0];
        if (isInteger(aa)) { 
          System.out.println("Integer type!"); 
        } 
        else{ 
          System.out.println("not Integer type!"); 
        } 
      }   static boolean isInteger(String strExp) {
        try {
          Long x = new Long(strExp);
        }
        catch (Exception e) {
          return false;
        }
        return true;
      }
    }这样好像更简单
      

  3.   

    我用的
    int num = 0;
    Scanner sc = new Scanner(System.in);try 
    {
       num = sc.nextInt();
    }
    catch(java.util.InputMismatchException e)
    {
        System.out.println("error");
    }好像也能做到
      

  4.   

    用循環比el稍微快點
    但我傾向于el因為代碼少