日期:2014-05-20  浏览次数:20842 次

String可以用match, 那int呢?
Java code
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();
        }


那int呢?
用什么?
Java code
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();                
        }


------解决方案--------------------
把int转成String就是了
------解决方案--------------------
你可以再定义一个String变量去做
------解决方案--------------------
已经是int了怎么可能出现小数点?
Java code
selection.matches("\\d+([.]\\d+)?")

------解决方案--------------------
可以先用String来判断,然后再转换成数值类型
Java code
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了貌似