/*获取键盘录入的纯数字,且不能超出int的范围*/
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
String str = null;
while((str=br.readLine())!=null){
if("over".equals(str))
break;
sb.append(str);
}
str = sb.toString()
System.out.println(str);
Pattern p = Pattern.compile("[0-9]*+");
Matcher m = p.matcher(sb);
if(!(m.matches())){
throw new RuntimeException("请输入纯数字!");
} else if(Integer.parseInt(str)>Integer.MAX_VALUE){
// throw new RuntimeException("请输入一个比"+Integer.MAX_VALUE+"小的数字");//此处怎么抛不出Runtime异常?
}
} |