zoukankan      html  css  js  c++  java
  • Java控制整形输入的法子

    第一种

    public static boolean func(String value) {
    		try {
    			Integer.parseInt(value);
    			return true;
    		} catch (Exception e) {
    			return false;
    		}
    	}
    	public static void main(String[] args) {
    		while (true) {
    			Scanner sc = new Scanner(System.in);
                      //調用方法
    			boolean s = func(sc.next());
    			if (s == true) {
    				System.out.println("输入的是数字");
    			} else {
    				System.out.println("输入的是不是数字");
    			}
    		}
    	}
    
    

    第二種:

          public static void main(String[] args) {
            Scanner sc;
            int i = 0;
            do {
                System.out.println("请输入VIP类型:
    1 一般類型
    2 超級VIP");
                sc = new Scanner(System.in);
                if (sc.hasNextInt()) {
                    i = sc.nextInt();
                }
            } while (i!=1&&i!=2);
        }
    
    
  • 相关阅读:
    【程序25】
    【程序24】
    【程序23】
    【程序22】
    【程序21】
    【程序20】
    【程序19】
    【程序18】
    string用法总结
    快速排序
  • 原文地址:https://www.cnblogs.com/zhenqk/p/13252477.html
Copyright © 2011-2022 走看看