问题:
程序:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31package javaapplication12; import java.util.Random; import java.util.Scanner; public class JavaApplication12 { public static int[] suiJi() {int []a=new int[100]; for(int i=0;i<100;i++) {Random a1=new Random(); a[i]=a1.nextInt(); } return a; } public static void main(String[] args) { Scanner input=new Scanner(System.in); try { int []a=new int[100]; int b; a=suiJi(); System.out.println("请输入数组下标:"); b=input.nextInt(); System.out.println("改下标对应的值是:"+a[b]); } catch(IndexOutOfBoundsException e){ System.out.println("Out of Bpumds"); } } } 在这里插入代码片
运行结果:
1)IndexOutOfBoundsException异常是免检异常,故可不用在方法名头后声明异常。
2)为了熟悉Random类顺便建立一个Random对象,而没有用(int)(Math.random()*100)方法。
最后
以上就是酷炫啤酒最近收集整理的关于java之IndexOutOfBoundsException异常练习的全部内容,更多相关java之IndexOutOfBoundsException异常练习内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复