日期:2014-05-20 浏览次数:20927 次
System.out.println("请选择运输方式:1.卡车 2.火车 3.飞机"); String method = scan.next(); while(!method.matches("\\d+([.]\\d+)?")||!method.matches("[1-3]")) { System.out.println("输入有误,请重新输入!"); method = scan.next(); }
System.out.println("请选择操作:1.查询 2.删除 3.新增"); Scanner scan = new Scanner(System.in); int selection = scan.nextInt(); while[color=#FF0000](!selection.matches("\\d+([.]\\d+)?")||[/color]selection<1||selection>3) { System.out.println("输入有误,请重新输入!"); selection = scan.nextInt(); } switch(selection) { case 1: display(); break; case 2: delete(); break; case 3: add(); }
selection.matches("\\d+([.]\\d+)?")
------解决方案--------------------
可以先用String来判断,然后再转换成数值类型
System.out.println("请选择操作:1.查询 2.删除 3.新增"); Scanner scan = new Scanner(System.in); String method = scan.next(); //int selection = scan.nextInt(); while[color=#FF0000](!selection.matches("\\d+([.]\\d+)?")||[/color]selection<1||selection>3) { System.out.println("输入有误,请重新输入!"); selection = scan.nextInt(); } int selection = (int)Float.parseFloat(method); switch(selection) { case 1: display(); break; case 2: delete(); break; case 3: add(); }
------解决方案--------------------
JAVA1.7已经支持使用case语句使用String了貌似