我正在尝试为棒球代码创建两个记分板

时间:2019-07-19 17:38:55

标签: java arrays

我是编码方面的初学者,所以请不要燃烧:)最近,我一直在研究一些棒球跟踪成绩。但是,我不知道我做得是否正确。

我已经有了代码的第一部分,但没有第二部分。

这是我的原始工作代码(输出发布在下面)

import java.io.*;
import java.util.*;

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,
            hits, 
            walks,
            outs,
            players,
            index,
            teamSize = 0;

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

        for (index = 0; index < LIST_LENGTH; index++) {
            team[index] = new Player(); 
        }

        while (fin.hasNext())
        {
            number = fin.nextInt();
            hits = fin.nextInt();
            walks = fin.nextInt();
            outs = fin.nextInt();
            index = findNumber(team, index, teamSize);

            if (index == -1 ) {
                team[teamSize].getNumber();
                team[teamSize].setNumber(number);
                team[teamSize].getHits();
                team[teamSize].setHits(hits);
                team[teamSize].getWalks();
                team[teamSize].setWalks(walks);
                team[teamSize].getOuts();
                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 findNumber(Player[] team, int index,int teamSize )
    {
        int Save = -1;
        for (int i = 0; i < teamSize - 1; i++) {
            if (index == team[i].getNumber())
                Save = i;
        }
        return Save; 
    }

    public static void displayArray(Player [] team, int team_size)
    {

        System.out.println("\n\nPlayer\tHits\tWalks\tOuts\n" 
                         + "------\t----\t-----\t----\n");

        for (int i = 0; i < team_size; i++)
        {
            System.out.println(team[i]);
        }
    }
} 

从逻辑上讲,我认为添加这些代码行会给我不同的计分板

Scanner fin2 = new Scanner(new FileReader("baseball2.txt"));

while (fin2.hasNext())
{
    number = fin2.nextInt();
    hits = fin2.nextInt();
    walks = fin2.nextInt();
    outs = fin2.nextInt();
    index = findNumber(team, index, teamSize);

    if (index == -1 ) {
        team[teamSize].getNumber();
        team[teamSize].setNumber(number);
        team[teamSize].getHits();
        team[teamSize].setHits(hits);
        team[teamSize].getWalks();
        team[teamSize].setWalks(walks);
        team[teamSize].getOuts();
        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()); 
    }
}

我还尝试将索引号更改为-2也不起作用

这就是原始输出的样子

This program tracks a baseball player's number and their
number of walks, runs and outs for each game in a season.



Player  Hits    Walks   Outs
------  ----    -----   ----

 1       2       2       2
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
 2       0       5       1
19       0       0       6
17       4       2       0
 9       3       2       1
 4       2       1       3
 3       1       2       3
 7       0       0       3

这就是我想要的样子

This program tracks a baseball player's number and their
number of walks, runs and outs for each game in a season.


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
Player  Hits    Walks   Outs
------  ----    -----   ----
1   2   2   2
2   0   5   7
3   3   3   6
4   3   3   6
7   0   0   6
8   1   4   1
9   3   2   1
10  2   2   2
11  6   0   0
12  2   2   2
17  4   2   0
18  4   2   0
19  0   5   7

但是如果我输入这两行代码,这就是输出

This program tracks a baseball player's number and their
number of walks, runs and outs for each game in a season.

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 20 out of bounds for length 20
        at Baseball8.main(Baseball8.java:67)

0 个答案:

没有答案