四则运算题目生成程序(基于控制台)(Bug修改)

针对上个程序中出现的bug进行修改
https://git.coding.net/cx873230936/calculator.git
Bug:
1.控制台输入问题数问题
a.不能处理用户输入负数。
b.当用户连续输入非数字符时程序自动退出。
c.控制台无输入时出现异常。

2.选择难度输入问题
a.当用户输入不为1或不为2的数时默认为1。
b.用户连续输入非数字符时程序自动退出。

Bug修改:

 String questNum=input.nextLine();
    while(!questNum.matches("\\d+"))//匹配数字
    {
    System.out.println("Please enter a number greater than 0!!");
    questNum = input.nextLine();
    }
    System.out.printf("u will test %d questions.\n",Integer.parseInt(questNum));
    System.out.println("easy or difficult?\n1、easy 2、difficult");
    String easyOrDifficultStr=input.nextLine();
    
    String regex="^[1|2]$";//匹配1或2
    Pattern pattern = Pattern.compile(regex);// 将给定的正则表达式编译到具有给定标志的模式中
    Matcher matcher = pattern.matcher(easyOrDifficultStr);// 模式进行匹配字符串
    while(!matcher.matches()){
    	System.out.println("Please enter 1 or 2!!");
    	easyOrDifficultStr=input.nextLine();
    	matcher = pattern.matcher(easyOrDifficultStr);
    }
    easyOrDifficult=Integer.parseInt(easyOrDifficultStr);

posted @ 2017-03-08 11:18  陈大熊  阅读(94)  评论(1编辑  收藏  举报