CODE
class PrintTeamScores
{
public static void main(String arg[])
{
int[] scores = new int[4]; // LINE A
scores[0] = 250;
scores[1] = 243;
scores[2] = 176;
scores[3] = 220; // LINE B
// LINE C
int total = 0;
for(int i = 0; i < scores.length; i++)
{
System.out.println("Team " + i + " = " + scores[i]);
total += scores[i];
}
System.out.println("Total = " + total);
}
}
Team 0 = 250
Team 1 = 243
Team 2 = 176
Team 3 = 220
Total = 889
Here we created an array scores
and initialized the values. In the for
loop, we have iterated through every element using index i
and printed the scores. Since the length of the array (scores.length
), is 4, the loop is executed for the values 0, 1, 2, and 3. While iterating the for
loop, we have also updated the total
to add the scores. That total
is printed after the for
loop.
4
to 6
i.e. change LINE A
to int[] scores = new int[4];
and also initialize the values scores[4]
and scores[5]
. See that the code below LINE C need not be changed irrespective of the number of teams.