229k views
3 votes
Assume that a Money variable yearlySales has also been declared. Write the necessary code that traverses the monthlySales array and adds it all up and stores the resulting total in yearlySales. Be sure make sure that yearlySales ends up with a valid value, i.e. a value of cents that is less than 100.

1 Answer

4 votes

Answer:

public class num3 {

public static void main(String[] args) {

double yearlySales =0;

int [] monthlySales = new int[12];

for(int i = 0; i<monthlySales.length; i++) {

yearlySales= yearlySales += monthlySales[i];

}

}

}

Step-by-step explanation:

Declare the variable yearlySales and initialize to 0.

Create an Array of ints monthlySales, set its length to 12

Using a for loop, add up all the elements in array and store in the varible yearly sales.

User SergeevDMS
by
3.8k points