Again and Again prints the same value

后端 未结 3 1811
孤街浪徒
孤街浪徒 2021-01-17 09:01

I have been asked to check the number of times a team\'s name is on the text that is on my computer. I wrote the code, the code works fine by counting the number of times th

相关标签:
3条回答
  • 2021-01-17 09:38

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

    0 讨论(0)
  • 2021-01-17 09:46

    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++;
    
            }
        }
    
    0 讨论(0)
  • 2021-01-17 10:00

    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++;
        }
    }
    
    0 讨论(0)
提交回复
热议问题