gpt4 book ai didi

java - 从 FOR 循环中提取返回值

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:45:22 26 4
gpt4 key购买 nike

我正在尝试实现一个方法,其返回值定义在 for 循环中。然而,我总是得到 eclipse 告诉我我必须初始化它。但如果我这样做,并将其设置为 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;
}

最佳答案

使用这个,去掉额外的变量,当然如果没有更多的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 循环中提取返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28169699/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com