创建对象数组需要帮助
我正在尝试制作一系列对象。我有两个类,一个是主类,另一个是方法类。我正在运行该程序的是 TestTrivia,具有 TestTrivia 所有方法的类是 Question。这个类的构造函数是 公共问题() { 问题=“空”; 答案=“空”; 点值 = 0; } 我正在尝试做这样的事情 人 personArray[] = new Person[5];
for(int i=0; i< personArray.length; i++)
{
personArray[i] = new Person();
}
所以我尝试过 问题 QuestionArray[] = 新问题[5]; for(int i=0; i< QuestionArray.length; i++) { QuestionArray[i] = new Question(); } 我尝试在两个类中使用它,即主类和充满方法的类。它应该进入主类,对吗?我得到的错误是整个 for 语句都带有下划线,并表示:非法开始类型找不到符号符号:类 i 位置:类 triviagame.TriviaGame 包 QuestionArray 不存在 triviagame 是包名称,“TriviaGame”是类名称,“Question”是另一个类名称。
I am trying to make an array of objects. I have Two classes, the main and another class for the methods. The one i am running the program in is TestTrivia, the class with all the methods for TestTrivia is Question. Constructor for this class is
public Question()
{
question = "null";
answer = "null";
pointValue = 0;
}
i am trying to do something like this
Person personArray[] = new Person[5];
for(int i=0; i< personArray.length; i++)
{
personArray[i] = new Person();
}
So i have tried
Question QuestionArray[] = new Question[5];
for(int i=0; i< QuestionArray.length; i++)
{
QuestionArray[i] = new Question();
}
I have tried to use it in both classes, the main and the one full of methods. It should go into the main class correct? The error i am getting is the whole for statement is underlined and says: illegal start type cannot find symbol symbol: class i location: class triviagame.TriviaGame package QuestionArray does not exist
triviagame is the package name, "TriviaGame" is the class name, "Question" is another class name.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
循环代码需要位于方法内,例如
main
。此外,如果它们不在同一个包中,您将需要导入不在“当前”包中的类。例如,如果
Question
位于默认包中,则需要将其导入到TriviaGame
类中。如果
Question
类也位于triviagame
包中,则不需要导入。The loop code needs to be within a method, like
main
.Also, if they are not in the same package, you will need to import classes not in the "current" package. For example, if
Question
is in the default package, it will need to be imported into theTriviaGame
class.If the
Question
class is in thetriviagame
package as well, the import is unnecessary.您试图在方法、构造函数或初始化块之外进行方法调用。不要这样做,而是确保您的代码位于正确的位置,例如在 main 或其他方法中。
You're trying to make methods calls outside of a method or constructor or initializer block. Don't do this, but instead make sure your code is in the proper location such as in the main or another method.