创建对象数组需要帮助

发布于 2024-12-17 04:19:35 字数 701 浏览 0 评论 0原文

我正在尝试制作一系列对象。我有两个类,一个是主类,另一个是方法类。我正在运行该程序的是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

海之角 2024-12-24 04:19:35

循环代码需要位于方法内,例如 main

此外,如果它们不在同一个包中,您将需要导入不在“当前”包中的类。例如,如果 Question 位于默认包中,则需要将其导入到 TriviaGame 类中。

package triviagame;

import Question;

public class TriviaGame {
    public static void main(String[] args) {
        System.out.println("Welcome to the New Age Trivia game");
        System.out.println("You will be asked five questions, let us begin");
        Question questionArray[] = new Question[5];
        for (int i = 0; i < questionArray.length; i++) {
            questionArray[i] = new Question();
        }
    }
}

如果 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 the TriviaGame class.

package triviagame;

import Question;

public class TriviaGame {
    public static void main(String[] args) {
        System.out.println("Welcome to the New Age Trivia game");
        System.out.println("You will be asked five questions, let us begin");
        Question questionArray[] = new Question[5];
        for (int i = 0; i < questionArray.length; i++) {
            questionArray[i] = new Question();
        }
    }
}

If the Question class is in the triviagame package as well, the import is unnecessary.

以往的大感动 2024-12-24 04:19:35

您试图在方法、构造函数或初始化块之外进行方法调用。不要这样做,而是确保您的代码位于正确的位置,例如在 main 或其他方法中。

public class TriviaGame {

   public static void main(String[] args) 
   {
      System.out.println("Welcome to the New Age Trivia game");
      System.out.println("You will be asked five questions, let us begin");
   }

   // this code is sitting out in the middle of no-where
   Question questionArray[] = new Question[5];
   for(int i=0; i< questionArray.length; i++)
   {
     questionArray[i] = new Question();
   }

}

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.

public class TriviaGame {

   public static void main(String[] args) 
   {
      System.out.println("Welcome to the New Age Trivia game");
      System.out.println("You will be asked five questions, let us begin");
   }

   // this code is sitting out in the middle of no-where
   Question questionArray[] = new Question[5];
   for(int i=0; i< questionArray.length; i++)
   {
     questionArray[i] = new Question();
   }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文