[JavaScript] 纯文本查看 复制代码
public class Demo06TryCatch {
public static void main(String[] args) {
int[] arr2 = {0, 1, 2, 6, 9, 10};
System.out.println("请输入索引:");
int index = new Scanner(System.in).nextInt();
//int element = getElement(arr2,index);
int element = print(arr2, index);
System.out.println(element);
}
private static int print(int arr[], int index) {
Scanner scanner = new Scanner(System.in);
int ele;
try {
ele = arr[index];
} catch (Exception e) {
System.out.println("重新输入索引");
ele = print(arr, scanner.nextInt());
}
return ele;
}
}