Reputation: 91
Here is my code. my objective is to print the current array bonusScores, but make the new values assigned to bonusScore[i] = bonusScores[i + 1] and leave the last element in the array at 40. i have two problems. my for loop makes the variable i go past the maximum number of elements the array can hold and my second problem is setting the last element to 40. Any ideas on how one would do this? Thanks!
public class StudentScores {
public static void main (String [] args) {
final int SCORES_SIZE = 4;
int[] bonusScores = new int[SCORES_SIZE];
int i = 0;
bonusScores[0] = 10;
bonusScores[1] = 20;
bonusScores[2] = 30;
bonusScores[3] = 40;
for(i = 0; i < SCORES_SIZE; i++){
bonusScores[i] = bonusScores[i] + bonusScores[i + 1];
}
for (i = 0; i < SCORES_SIZE; ++i) {
System.out.print(bonusScores[i] + " ");
}
System.out.println();
return;
}
}
Upvotes: 1
Views: 96
Reputation: 91
Nevermind guys I figured out my code problem thanks guys all i had to do was change
for(i = 0; i < SCORES_SIZE; i++){
bonusScores[i] = bonusScores[i] + bonusScores[i + 1];
}
to
for(i = 0; i < SCORES_SIZE-1; i++){
bonusScores[i] = bonusScores[i] + bonusScores[i + 1];
}
Upvotes: 1