一个方法里能不能有两次try catch

来源:3-2 主方法的实现2

jimam

2019-07-23 14:28:02

public int[] insertData() {

System.out.println("要输入几个成绩");

Scanner sc = new Scanner(System.in);

try {

int k = sc.nextInt();

} catch (java.util.InputMismatchException e) {

System.out.println("输入的数据有误,请重新");

sc.next();

}

int[] a = new int[k];

for (int i = 0; i < a.length - 1; i++) {

System.out.print("请输入第" + (i + 1) + "个数据:");

try {

a[i] = sc.nextInt();

} catch (java.util.InputMismatchException e) {

System.out.println("输入的数据有误,请重新");

sc.next();

continue;

}

}

return


a;

}



写回答

1回答

好帮手慕酷酷

2019-07-23

同学,你好,一个方法里可以存在两个try catch语句块,方法里对try catch语句块没有数量的限制。如果解决了你的疑惑,请采纳,祝学习愉快~ 


0
himam
h 但是像我这么写的话 下面的int[] a = new int[k]; 识别不到 k 我不知道有没有什么方法
h019-07-23
共1条回复

0 学习 · 11489 问题

查看课程