日期:2014-05-20 浏览次数:21072 次
public static void main(String[] args) {
int i = 0;
Scanner sc = new Scanner(System.in);
do {
try {
String str = sc.nextLine();
int x = Integer.parseInt(str);//靠异常来处理的,你也可以用正则表达式判断啥的判断是不是可以转为整数
System.out.println(x);
} catch(Exception e) {
System.out.printf("出错");
continue;
}
i ++;
} while(i<6);
}
------解决方案--------------------
Scanner sc = new Scanner(System.in);
while(true){
System.out.println("请输入一个整数");
String str = sc.next();
if(str.matches("[0-9]*")){
System.out.println("您很聪明输入的整数是:" + Integer.parseInt(str));
} else{
System.err.println("看清除了要输入的是整数");
}
}
------解决方案--------------------
我只会最本的方法,转换成int判断值对不对不就行了