更新数组列表

时间:2017-05-30 22:06:18

标签: java arrays

程序是从包含玩家编号和统计数据的文本文件中获取输入。如果玩家已经在列表中,我想更新该玩家的统计数据。我在确定玩家是否已经在列表中的方法时遇到问题。当前方法无论如何打印所有玩家。我的代码在主类或方法中存在问题吗?

public class Baseball8
{

    public static void main(String[] args) throws FileNotFoundException
    {

    Scanner fin = new Scanner(new FileReader("baseball.txt"));

    final int LIST_LENGTH = 20;

    int number = 0,         // number, hits, walks, outs
        hits = 0,
        walks = 0,
        outs = 0,
        players = 0,
        index = 0,
        teamSize = 0;
        playerIndex = 0;


    System.out.println("This program tracks a baseball player's number "
                         + "and their\number of walks, runs and outs for "
                         + "each game in a season.\n");


    Player team[] = new Player[LIST_LENGTH];

    int i;

    for(i = 0; i < LIST_LENGTH; i++)
        team[i] = new Player();



while (fin.hasNext())
    {
           number = fin.nextInt();
           hits = fin.nextInt();
           walks = fin.nextInt();
           outs = fin.nextInt();


        System.out.println(number + " " + hits + " " + walks + " " + outs + " ");



        playerIndex = findPlayerIndex(team,teamSize,number);


        if(playerIndex == -1)
        {
            team[teamSize].setNumber(number);
            team[teamSize].setHits(hits);
            team[teamSize].setWalks(walks);
            team[teamSize].setOuts(outs);
            teamSize++;
        }

        else
        {
            team[index].setHits(hits + team[index].getHits());
            team[index].setWalks(walks + team[index].getWalks());
            team[index].setOuts(outs + team[index].getOuts());
        }
   }
displayArray(team, teamSize);
fin.close();

}

public static int findPlayerIndex(Player p[], int n, int number)
{
    int i;

    for(i = 0; i < n; i++);
    {
        if(p[i].getNumber() == number)
            return i;
       else
           return -1;
    }
}


    **Current output:**
//player hits, walks, outs are added to array[0]
     Player Hits    Walks   Outs
------  ----    -----   ----

 1   5  10  18     <
19   0   5   1
 2   0   0   6
18   4   2   0
 4   1   2   3
12   2   2   2
 7   0   0   3
 8   1   4   1
10   2   2   2
 3   2   1   3
11   6   0   0
17   4   2   0
 9   3   2   1


Desired output:
//stats added to array[i]
Player  Hits    Walks   Outs
------  ----    -----   ----

 1   2   2   2
19   0   5   7 <
 2   0   5   7
18   4   2   0
 4   3   3   6 <
12   2   2   2
 7   0   0   6 <
 8   1   4   1
10   2   2   2
 3   3   3   6 <
11   6   0   0
17   4   2   0
 9   3   2   1

2 个答案:

答案 0 :(得分:3)

您的功能 findPlayerIndex

有两个问题
  • 删除;在for so循环之后可以工作:for(i = 0; i&lt; n; i ++); //删除;

  • 循环结束后返回-1, int i 可以在循环命令中声明。像这样:

    for (int i = 0; i < n; i++) {
        if(p[i].getNumber() == number)
           return i;      
    }
    
    return -1;
    

玩得开心!

答案 1 :(得分:2)

您需要在循环之后而不是循环内返回-1。即。

for(i = 0; i < n; i++)
{
     if(p[i].getNumber() == number)
         return i;    
}
return -1;

这基本上确保您已检查整个数组,然后如果for循环中的谓词不符合,则将返回-1