gpt4 book ai didi

java - 下面的 Java 程序的输出是什么?为什么我会收到错误

转载 作者:行者123 更新时间:2023-12-02 03:58:57 24 4
gpt4 key购买 nike

如果我执行以下程序,我会收到错误消息。它说 o 无法解析为变量。

public class Test {

/**
* @param args
*/
public static void main(String[] args) {

try{
int o[] = new int[2];
o[3]=23;
o[1]=33;
}catch(Exception e){
System.out.println(e.getMessage());
e.printStackTrace();
}

System.out.println(o[1]); //THis line shows the error.
}

}

为什么我要排队 System.out.println(o[1]);

最佳答案

首先,您在 try block 内初始化 o,因此 o 在其外部不可见。更改此设置并调用 o[3] 将给出 ArrayIndexOutOfBounds,因为 o 的大小仅为 2。

关于java - 下面的 Java 程序的输出是什么?为什么我会收到错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35104691/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com