Write a loop that sets each array element to the sum of itself and the next element, except for the last element which stays the same. Be careful not to index beyond the last element. Ex:
Initial Scores: 10, 20, 30, 40
Scores after loop: 30, 50, 70, 40
Import. java.util.Scanner;
public class StudentScores {
public static void main (String [] args){
Scanner scnr = new Scanner (System.in);
final int SCORES_SIZE = 4;
int [] bonusScores = new int[SCORES_SIZE];
int i;
for ( i = 0; i < bonusScores.length; ++i ){
bonusScores[i] = scnr.nextInt();
}
/* your solution goes here */
for ( i=0; i < bonusScores.length; ++i ){
System.out.print(bonusScores[i] + " ");
}
System.out.println();
}
}
import java.util.Scanner; public class StudentScores { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); final int SCORES_SIZE = 4; int[] bonusScores = new int[SCORES_SIZE]; int i; for (i = 0; i < bonusScores.length; ++i) { bonusScores[i] = scnr.nextInt(); } for (i = 0; i < bonusScores.length-1; i++) { bonusScores[i] += bonusScores[i+1]; } for (i = 0; i < bonusScores.length; ++i) { System.out.print(bonusScores[i] + " "); } System.out.println(); } }
Get Answers For Free
Most questions answered within 1 hours.