读书人

String可以用match 那int呢?解决思

发布时间: 2012-02-13 17:20:26 作者: rapoo

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了貌似

读书人网 >J2SE开发

热点推荐