编写一个类,在主 main 方法中定义字符串 messages,数组包括 3 个元素:“您好!!”,“早上好!!”,“祝你好运!!”;使用 while 循环遍历字符串数组 messages,输出数组元素。要求如下:
(1)定义整型循环变量 num,初始值为 0,使用 while (num<4)循环遍历字符串数组 message。
(2)在 while 循环中使用 try-catch 捕获 ArraylndexOutOfBoundsException(数组越界)异常。
(3)在 catch 中输出“越组越界”信息。
public static void main(String[] args) {
String[] message = {"您好!!", "早上好!!", "祝你好运!!"};
int num = 0;
while (num < 4) {
try {
System.out.println(message[num++]);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("越组越界");
}
}
}