【问题标题】:"Cannot find symbol" - class with the main method can call methods from one of the other classes but not the second of the others?“找不到符号” - 具有主要方法的类可以调用其他类之一的方法,但不能调用其他类的方法?
【发布时间】:2011-05-21 12:52:18
【问题描述】:

我已经在这里潜伏了一段时间,但是我遇到了一个问题,我在为作业编写的一些 Java 程序中无法解决。我敢打赌,它们并不太难弄清楚,但我就是不明白。

我遇到了类似这样的错误:

RugbyTeamLadderEditor.java:125: cannot find symbol
symbol  : method findAveragePoints(java.util.ArrayList<RugbyTeam>)
location: class RugbyTeamLadderEditor
                        double averagePointsToBePrinted = findAveragePoints(rugbyTeams);

我有三个类,从具有主要方法的类(RugbyTeamLadderEditor)中,我可以调用构造函数类,但不能调用其中包含一些方法的其他类(第 1 部分)。我应该对包裹做些什么吗? - 我只知道在我正在做的这个介绍性编程课程中我没有学到任何关于包的知识,而且我不确定如果我使用它们会如何收到它们。

我的代码有几百行,所以我把它们放在了 pastebin 中——我希望我这样做没有违反任何错误:/ 每个类都在它自己的 .java 文件中。

http://pastebin.com/FrjYhR2f

干杯!

编辑:我的代码的一些片段:

在 RugbyTeamLadderEditor.java 中:

// if the identification number is equal to 5, then print out the average points of all of the teams in the ArrayList
    else if (identificationNumber == 5)
    {
        double averagePointsToBePrinted = findAveragePoints(rugbyTeams);
    }

在 Part1.java 中:

/**
 * This method takes a RugbyTeam ArrayList and returns a
 * double that represents the average of the points of all
 * of the rugby teams
 */
public static double findAveragePoints(ArrayList<RugbyTeam> rugbyTeams)
{
    // If there are no objects in the ArrayList rugbyTeams, return 0
    if (rugbyTeams.size() == 0)
        return 0;

    // Declare a variable that represents the addition of the points of each team;
    // initialise it to 0
    double totalPoints = 0;

    // This is a code-cliche for traversing an ArrayList
    for (int i = 0; i < rugbyTeams.size(); i++)
    {
        // Find then number of points a team has and add that number to totalPoints
        RugbyTeam r = rugbyTeams.get(i);
        totalPoints = totalPoints + r.getPoints();
    }

    // Declare a variable that represents the average of the points of each teams, 
    // i.e. the addition of the points of each team divided by the number of teams 
    // (i.e. the number of elements in the ArrayList); initialise it to 0
    double averagePoints = totalPoints / rugbyTeams.size();
    return averagePoints;

}

它还没有完全完成——我仍然需要放入一个 print 语句来打印那个双精度,但现在这无关紧要,因为我实际上无法让那个双精度具有一个值。

【问题讨论】:

  • 发布您的代码片段。然后人们可以提供更好的帮助。
  • 谢谢,这是个好主意 - 我发布了一些相关的代码,我确信我能够推断出我应该如何处理其他受影响的代码(其中我已经删除了,只是因为它们不起作用 - 一旦我知道该怎么做,我会从头开始写)。

标签: java package symbols


【解决方案1】:

您正在尝试调用方法findAveragePoints。根据您所说的当前实现,该方法将在类RugbyTeamLadderEditor 中找到。但是该方法是在类Part1 中定义的。因此,要完成这项工作,您需要在对方法的调用前加上 Part1.(因为它是一个静态方法)并且程序应该可以工作。


编辑

代码基本上是这样的

double averagePointsToBePrinted = Part1.findAveragePoints(rugbyTeams);

此外,每次您尝试调用在另一个类中定义的方法时,您都必须提供此类的实例或添加类的名称(如此处 Part1)到被调用的方法。

作为一个侧节点,您应该更改变量的名称quitProgram。变量的名称及其含义相互矛盾。因此,为了让阅读代码的人更清楚,您应该更改名称或处理方式。

【讨论】:

  • 我不太确定你的意思是“在调用第 1 部分的方法之前” - 这是什么意思,我应该怎么做?非常感谢您的评论,顺便说一句!我不是很擅长这个;我主要是通过讲义来度过难关,但到目前为止一切都很好。
  • 将该方法调用为Part1.findAveragePoints(rugbyTeams)。这是Part1 类的静态方法,而不是RugbyTeamLadderEditor
  • 哈,这很容易!我以为我将不得不处理包裹或其他东西 - 非常感谢!编辑:另外,适当地注意到了 quitProgram 变量 - 干杯
猜你喜欢
  • 1970-01-01
  • 2017-06-25
  • 2019-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多