简体   繁体   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?

I've been lurking here for a little while, but I've come into a problem that I can't solve in some Java programs I'm writing for an assignment. 我已经潜伏了一段时间,但是遇到了一个我在为任务编写的Java程序中无法解决的问题。 I bet they're not too difficult to figure out, but I'm just not getting it. 我敢打赌他们不太难弄清楚,但我只是不明白。

I've been getting errors along the lines of this: 我一直在这样的错误:

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

I have three classes, and, from the class with the main method (RugbyTeamLadderEditor), I can call the constructor class, but not the other class which has some methods in it (Part1). 我有三个类,并且从具有主要方法的类(RugbyTeamLadderEditor)中,我可以调用构造函数类,但不能调用其中包含某些方法的另一个类(第1部分)。 Should I be doing something with packages? 我应该对包裹做些事吗? - all I know is that I didn't learn anything about packages in this introductory programming course that I'm doing, and I'm not sure how they would be received if I were to use them. -我所知道的是,在我正在做的入门编程课程中,我对软件包没有任何了解,而且我不确定如果要使用它们将如何得到它们。

My code is a couple of hundred lines long, so I put them in pastebin - I hope I haven't transgressed any faux pas by doing this :/ Every class is in its own .java file. 我的代码长了几百行,所以我将它们放在pastebin中-我希望这样做不会违反任何假冒的伪指令:/每个类都在其自己的.java文件中。

http://pastebin.com/FrjYhR2f http://pastebin.com/FrjYhR2f

Cheers! 干杯!

EDIT: a few fragments of my code: 编辑:我的代码的几个片段:

In RugbyTeamLadderEditor.java: 在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);
    }

In Part1.java: 在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;

}

It's not quite finished yet - I still need to put a print statement in to print that double, but it's irrelevant for now because I can't actually get that double to take on a value. 还没有完全完成-我仍然需要输入打印语句来打印该double,但是现在无关紧要,因为我实际上不能让double取值。

Your are trying to call the method findAveragePoints . 您正在尝试调用方法findAveragePoints With the current implementation you say, that the method will be found in the class RugbyTeamLadderEditor . 您说的是当前实现,该方法将在类RugbyTeamLadderEditor找到。 But the method is defined in the class Part1 . 但是该方法在类Part1定义。 So to make this work you prepend the call to the method with Part1. 因此,为完成这项工作,您需要在Part1.预先调用该方法Part1. (since it is a static method) and the program should work. (因为它是静态方法),所以程序应该可以工作。


EDIT 编辑

The code would basically look like this 该代码基本上看起来像这样

double averagePointsToBePrinted = Part1.findAveragePoints(rugbyTeams);

Also every time you try to call a method that is defined in another class than the current you either have to provide an instance of this class or prepend the name of the class (like here Part1 ) to the method called. 同样,每次尝试调用在当前类之外的其他类中定义的方法时,您都必须提供此类的实例,或者将类的名称(例如Part1 )放在所调用的方法之前。

As a side node you should change the name of your variable quitProgram . 作为副节点,您应该更改变量quitProgram的名称。 The name of the variable and its meaning contradict each other. 变量的名称及其含义相互矛盾。 So to make things more clear for anyone reading the code you should change either the name or the handling. 因此,为使阅读代码的人更清楚,您应该更改名称或处理方式。

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

相关问题 jar并且找不到方法的符号(但是找到了其他类方法)? - jar and cannot find symbol for method (but other class methods are found)? 在新的 class 中创建方法; 在主 class 中找不到符号错误 - creating method in new class; cannot find symbol error in main class Kotlin:找不到符号类 Fragment 或其他 android 类 - Kotlin : Cannot find symbol class Fragment or other android classes 在主类中找不到符号 - Cannot find symbol in main class 错误找不到符号Main(); 第20行符号:方法Main()位置类Main 1错误 - Error Cannot find Symbol Main(); Line 20 Symbol: Method Main() Location class Main 1 error 我的main方法中的哪条语句正在调用我的其他类和main类中的所有其他方法? - Which statement in my main method is calling all my other methods in my other classes and my main class? 仅在主void中查找方法(找不到符号,符号:方法tulosta(),位置:类Object) - Finds method only in main void (cannot find symbol, symbol: method tulosta(), location: class Object) 如何将其他方法中的字符串调用到main方法中? - How to call strings from other methods into main method? Java-找不到其他类的符号错误 - Java - Cannot Find Symbol Error With Other Classes 错误:使用西里尔符号调用JAXB类的方法时找不到符号 - error: cannot find symbol when call JAXB class's methods with Cyrillic symbol
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM