发布回数组

时间:2014-03-07 14:51:59

标签: java arrays

public class TeamInfo {

    public static void main(String[] args){

        String[][] teamInfo = new String[5][2];
        String[] teams = {"Team Penguin","Team Go Bokke","Team Pink","Team Orange","Team Os"};
        int[] scores = {232,315,0,185,388};
        for(int i = 0; i < teamInfo.length; i++){
            for(int j = 0; j < 2; j++){
                teamInfo[i][0] = teams[i];
                teamInfo[i][1] = String.valueOf(scores[i]);
            }
        }
        System.out.println("Team ------ Score");
        for(int i = 0; i < teamInfo.length; i++){
            System.out.printf("%s ------- %s\n",teamInfo[i][0],teamInfo[i][1]);
        }

    }
}

现在,我想获得帮助,我必须为第1组和第3组添加新分数并将其发回数组。

1 个答案:

答案 0 :(得分:1)

首先,使用j的第二个循环什么都不做。取下环并保持身体。然后,要更改团队x的分数,请执行:

teamInfo[x][1] = String.valueOf(newScore);

不使用索引值,而是引入常量,如:

public static final int TEAM_NAME_INDEX = 0;
public static final int TEAM_SCORE_INDEX = 1;

然后写:

teamInfo[x][TEAM_SCORE_INDEX] = String.valueOf(newScore);