Java控制整形输入的法子

时间:2020-07-06 01:32:49   收藏:0   阅读:88

第一种

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("输入的是不是数字");
			}
		}
	}

第二種:

         Scanner sc;
	int i = 0;
	do {
		System.out.println("请输入VIP类型:\n1 一般類型\n2 超級VIP");
		sc = new Scanner(System.in);
		if (sc.hasNextInt()) {
			i = sc.nextInt();
		}
	} while (i!=1||i!=2);

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!