+-

我正在尝试实现一种其返回值定义为for循环的方法.但是,我总是会告诉我必须初始化它.但是,如果我这样做,并将其设置为null,则它始终保持null …我在做什么错?
public String getPoints(String team){
String teamName;
String outcome;
for ( FootballClub club : league )
{
teamName = club.getName();
if ( teamName.trim().toLowerCase().equals( team ) )
{
outcome = ( team + " " + club.getPoints() ) + " Points";
}
}
return outcome;
}
最佳答案
使用此方法,可以消除多余的变量,当然,如果不再有带有结果的操作:
public String getPoints(String team){
String teamName;
for ( FootballClub club : league )
{
teamName = club.getName();
if ( teamName.trim().toLowerCase().equals( team ) )
{
return ( team + " " + club.getPoints() ) + " Points";
}
}
return "null or some string if .equals( team ) false for all clubs";
}
点击查看更多相关文章
转载注明原文:java-从FOR循环中提取返回值 - 乐贴网