![](/img/trans.png)
[英]jar and cannot find symbol for method (but other class methods are found)?
[英]“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文件中。
干杯!
編輯:我的代碼的幾個片段:
在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.