import java.util.Scanner;//输入int型数据5个,类型符合则输出数,不符合则输出error
public class yichang {//为什么,输入“r”后程序就输出多个“error”结束
public static void main(String[] args) {//而不能继续输入下一个数据
Scanner cjl=new Scanner(System.in);
for(int i=0;i<5;i++){
try{
System.out.println(cjl.nextInt());
}catch(Exception e){
System.out.println("error");
}
}
}
} 先谢谢啦!目的达到:
输入:
4
5
f
6
t
输出:
4
5
error
6
error
public class yichang {//为什么,输入“r”后程序就输出多个“error”结束
public static void main(String[] args) {//而不能继续输入下一个数据
Scanner cjl=new Scanner(System.in);
for(int i=0;i<5;i++){
try{
System.out.println(cjl.nextInt());
}catch(Exception e){
System.out.println("error");
}
}
}
} 先谢谢啦!目的达到:
输入:
4
5
f
6
t
输出:
4
5
error
6
error
public class yichang {//为什么,输入“r”后程序就输出多个“error”结束
public static void main(String[] args) {//而不能继续输入下一个数据
Scanner cjl=new Scanner(System.in);
for(int i=0;i<5;i++){
try{
System.out.println(cjl.nextInt());
}catch(Exception e){
cjl.next();
System.out.println("error");
}
}
}
}
nextInt()是转INT吧
不转型直接输出把。
System.out.println(cjl.nextInt());
}catch(Exception e){
System.out.println("error");
}
看你这个,不知道你想干什么。1.如果只是想要得到下面那样的结果,你就用if(){}else{}2.你这样写,如果真有异常,程序就直接结束了,不会再循环了吧,如果你非要这么干,试试这个行不行
try{
System.out.println(cjl.nextInt());
}catch(Exception e){throw new RuntimeException("XXX", e);
System.out.println("error");
}