运动时间表未显示字符串

时间:2018-11-09 20:33:59

标签: java

这是一个体育联盟计划,一组团队通过游戏时间表进行比赛并确定获胜者。

程序运行良好,直到最终输出不会让我看到获胜者,而是显示了这一点:

  

本赛季获得9分的冠军:[Ljava.lang.String; @ e73f9ac

我将其更改为teams.length,这使程序正常工作,但是它会向我显示团队(i)的编号,而不是像“ Vancouver”这样的字符串名称。

谢谢。

    }

    int peak = 0;                                
    int[] total = new int[teams.length];         

    for (int i=0; i<teams.length; i++){                    
      total[i] = 2*wins[i]+ties[i]; 
      if (total[i] > peak) peak = total[i];

      System.out.println(teams[i]+" - " + wins[i] + " wins, " + losses[i] + " losses, " + ties[i] + " ties = " + total[i]);
  }    

  System.out.println("The season winner(s) with " + peak + " points: " + teams);  

  for (int i=0; i<teams.length; i++){
    if (peak < total[i]) peak = total[i];
  }      

}

static int indexOfTeam(String team, String[] teams){  
  for (int i=0; i<teams.length; i++)
    if (team.compareTo(teams[i]) == 0) return i;
    return -1;
  }
}

3 个答案:

答案 0 :(得分:1)

您将打印teams数组,而不是打印获胜团队。
在除高峰期外迭代商店时,获胜团队的指数:

 int index = -1;
 for (int i=0; i<teams.length; i++){                    
     total[i] = 2*wins[i]+ties[i]; 
     if (total[i] > peak) {
        index = i;
        peak = total[i];
     }
     System.out.println(teams[i]+" - " + wins[i] + " wins, " + losses[i] + " 
   losses, " + ties[i] + " ties = " + total[i]);
 } 

最后:

System.out.println("The season winner(s) with " + peak + " points: " + 
 teams[index]);

答案 1 :(得分:0)

teams是一个字符串数组,必须在名称后插入索引

答案 2 :(得分:0)

如果您要打印所有使用Arrays.toString(teams)的团队,但我认为您只希望打印部分团队数组,因此可以创建获胜者列表

 List<String> winners = new ArrayList<Integer>;
 for (int i=0; i<teams.length; i++){                    
     total[i] = 2*wins[i]+ties[i]; 
     if (total[i] > peak) {
        winners.add(teams[i]);
        peak = total[i];
     }
 System.out.println(teams[i]+" - " + wins[i] + " wins, " + losses[i] + " 
  losses, " + ties[i] + " ties = " + total[i]);
} 

可以简单地打印索引对象

System.out.println("The season winner(s) with " + peak + " points: " +winners);

如果您想要一个数组,则可以对获胜者对象使用toArray()