Again and Again prints the same value

落花浮王杯 提交于 2019-12-01 14:55:02

Calling getName() once every loop will cause the program to ask for a team name every loop:

        int count = 0;
        for ( int  index = 0 ; index < winners.length ; index ++ )
        {
            if ( getName(teamName).equals(winners[index]))
            {
                count++;

            }
        }

By moving getName() out of the loop, it will only be called once (and a team name will only be requested once):

    int count = 0;
    String nameOfTeam = getName(teamName); // This line runs getName() once

    for ( int  index = 0 ; index < winners.length ; index ++ )
    {
        if ( nameOfTeam.equals(winners[index]))
        {
            count++;

        }
    }

Don't call 'GetName' in the loop, call it once before the loop and store the result.

In the method checkSeries1() remove the method call for getName(teamName) out of for loop and call getName() only once outside for loop, like this:

int count = 0;
String name = getName(teamName);
for ( int  index = 0 ; index < winners.length ; index ++ )
{
    if ( name.equals(winners[index]))
    {
        count++;
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!