“找不到符号” - 具有 main 方法的类可以调用其他类之一的方法,但不能调用其他类中的第二个方法?
我在这里潜伏了一段时间,但是在我为作业编写的一些 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 文件中。
干杯!
编辑:我的代码的一些片段:
在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 值。
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. 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). 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.
Cheers!
EDIT: a few fragments of my code:
In 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:
/**
* 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在尝试调用
findAveragePoints
方法。根据您所说的当前实现,该方法将在类RugbyTeamLadderEditor
中找到。但该方法是在类Part1
中定义的。因此,为了使这项工作正常进行,您可以在对该方法的调用前添加Part1.
(因为它是静态方法),并且程序应该可以正常工作。编辑
代码基本上看起来像这样
此外,每次您尝试调用在当前类之外的另一个类中定义的方法时,您要么必须提供此类的实例,要么在前面添加类的名称(就像这里的Part1)到被调用的方法。
作为侧节点,您应该更改变量
quitProgram
的名称。变量的名称和它的含义是相互矛盾的。因此,为了让阅读代码的人更清楚,您应该更改名称或处理方式。Your are trying to call the method
findAveragePoints
. With the current implementation you say, that the method will be found in the classRugbyTeamLadderEditor
. But the method is defined in the classPart1
. So to make this work you prepend the call to the method withPart1.
(since it is a static method) and the program should work.EDIT
The code would basically look like this
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.
As a side node you should change the name of your variable
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.