gpt4 book ai didi

java - 使用数组从另一个类调用方法

转载 作者:行者123 更新时间:2023-11-30 08:20:52 25 4
gpt4 key购买 nike

我需要有关在 main 方法中使用数组从定义类调用方法的帮助。

public class Student{
private String id;

public void setId(String id){
this.id=id;
}
public String getId(){
return id;
}

}

总的来说,有一群像这样的学生,

public static void main(String[] args){
int numOfStudent=0;
Student[] students = new Student[numOfStudent];

students[numOfStudent].setId(JOptionPane.showInputDialog("Enter id:"));
numOfStudent++;

}

我不断收到一条错误消息说

"java:6: error: cannot find symbol

students[numOfStudent].setId(JOptionPane.showInputDialog("Enter id:"));

symbol: variable JOptionPane location: class Whatever

1 error "

这里有什么问题??

最佳答案

编译器认为 JoptionPane 是一个变量(你没有声明)。

您需要在文件顶部导入此类。

此外,您将获得 ArrayIndexOutOfBoundsException,因为您的数组的长度为 0,因此没有元素(在索引 0 或任何其他索引处)。

关于java - 使用数组从另一个类调用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25598974/

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