在主方法java中调用时找不到方法
我创建了一个方法来检查团队阵列中的所有团队,看看谁的获胜百分比最高。一旦循环找到了最高的获胜百分比,它应该以该获胜百分比回报球队。当我在main方法中调用一行简单的代码时在主方法java中调用时找不到方法,java,class,methods,Java,Class,Methods,我创建了一个方法来检查团队阵列中的所有团队,看看谁的获胜百分比最高。一旦循环找到了最高的获胜百分比,它应该以该获胜百分比回报球队。当我在main方法中调用一行简单的代码时 System.out.println("The team with the highest winning percentage is: " +highest(tm)); 我得到一个编译错误,说它找不到最高的方法。它是静态的,因此它可以与团队tm阵列的主要方法进行通信。如果我不明白,请向我解释我的误解 public clas
System.out.println("The team with the highest winning percentage is: " +highest(tm));
我得到一个编译错误,说它找不到最高的方法。它是静态的,因此它可以与团队tm阵列的主要方法进行通信。如果我不明白,请向我解释我的误解
public class teams{
public static void main(String [] argv){
/*Team team1 = new Team("knicks");
Team team2 = new Team("nets");
team1.play(team2);
team1.play(team2);
team2.play(team1);
team2.printrecord();
team1.printrecord();
team2.winpercent();*/
String[] teamnames = {"knicks", "nets", "lakers", "celtics", "heat", "spurs"};
Team[] tm = new Team[teamnames.length]; // creates an array of teams
for (int i=0;i<teamnames.length;i++){ // assigns a team to each string in teamnames
tm[i] = new Team(teamnames[i]);
}//for
for (int i=0;i<teamnames.length;i++){ //nested for loop to have each team play another team once
for(int k=i+1; k<teamnames.length;k++){
if(k!=i)
tm[i].play(tm[k]);
}//nestedfor
}//for
System.out.println("The team with the highest winning percent is: " +highest(tm));
}//main
}//teams
class Team{
double wins;
double losses;
double winningpercent;
String name;
public Team(String n){
name = n;
wins = 0;
losses = 0;
winningpercent = 0;
}//constructor
public void lose(){
losses++;
}//losses
public void win(){
wins++;
}//wins
public void printrecord(){
System.out.println("The W-L record for the " +name+ " is: " +String.format("%d",(long)wins)+"-"+String.format("%d",(long)losses));
}
public void play(Team j){
if((Math.random())<0.5){
System.out.println("The "+j.name+" Have Won!");
j.win();
this.lose();
}//if
else {
System.out.println("The "+name+" Have Won!");
this.win();
j.lose();
}//else
}//play
public double winpercent(){
double winningpercentage = (wins/(losses+wins))* 100;
System.out.println("The Winning percentage for the " +name+" is: " +winningpercentage+"%");
this.winningpercent = winningpercentage;
return winningpercentage;
}//winningpercent
public static String highest(Team[] tm){
String highest = "";
for (int i=0;i<tm.length;i++){
for(int k=i+1;k<tm.length;k++){
if (k!=i && tm[i].winningpercent > tm[k].winningpercent)
highest = tm[i].name;
}//nestedforloop
}//forloop
return highest;
}//highest
}//Team
对。首先,该方法不是静态的。那么这个
public String highest(Team[] tm){
应该是
public static String highest(Team[] tm){
然后需要使用类名或导入静态方法-
System.out.println("The team with the highest winning percent is: " +
Team.highest(tm));
团队类中最高级别的方法不是静态的。您需要团队类的一个实例。new team.highesttmi使该方法保持静态,但它仍然给我相同的错误。但我明白为什么我必须让它成为一个新的instance@BenjiWeiss-因为您的静态方法不能有Team类型的参数,所以这些方法是通过类的加载加载的!此时未加载任何对象或实例!如果将该方法设置为静态,则可以通过类名Team.highesttm访问它。请看下面的答案。好的,现在我已经编译了它,它可以运行了,但是那一行的输出是空字符串。您的字段永远不会更新。改变如果k=i&&tm[i]。winningpercent>tm[k]。winningpercent到if k=i&&tm[i].winpercent>tm[k].winpercent好的,谢谢你的帮助。我不知道为什么它给了我一个错误的团队,虽然,我运行它。。据说凯尔特人队在赢得3场比赛时的得分最高,而在同一轮比赛中,湖人队赢了4场比赛。这意味着湖人队应该是他们的输出。。有什么见解吗?我试图找出为什么它给了我错误的结果,但我似乎找不到it@BenjiWeiss覆盖团队中的toString,并使用Arrays.toString打印团队。你的假设一定是错误的。我明白了,这是一个简单的误解,仅仅因为湖人赢了更多的比赛,并不意味着他们的胜利率更高,因为他们输掉的比赛也比凯尔特人多。