繁体   English   中英

“找不到符号”-具有main方法的类可以从其他一个类中调用方法,而不能从其他另一个中调用方法?

[英]“Cannot find symbol” - class with the main method can call methods from one of the other classes but not the second of the others?

我已经潜伏了一段时间,但是遇到了一个我在为任务编写的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;

}

还没有完全完成-我仍然需要输入打印语句来打印该double,但是现在无关紧要,因为我实际上不能让double取值。

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


编辑

该代码基本上看起来像这样

double averagePointsToBePrinted = Part1.findAveragePoints(rugbyTeams);

同样,每次尝试调用在当前类之外的其他类中定义的方法时,您都必须提供此类的实例,或者将类的名称(例如Part1 )放在所调用的方法之前。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM