- 二维数组的练习
- 某商城每个季度的营业额如下:单位(万元)
- 第一季度:22,66,44
- 第二季度:77,33,88
- 第三季度:25,45,65
- 第四季度:11,66,99
- 要求计算出每个季度的总营业额和全年的总营业额
package _exercis; public class TwoArray { public static void main(String[] args) { int eachSum1 = 0; int eachSum2 = 0; int eachSum3 = 0; int eachSum4 = 0; int totalSum = 0; int[][] arr = { {22, 66, 44}, {77, 33, 88}, {25, 45, 65}, {11, 66, 99} }; for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr[i].length; j++) { totalSum += arr[i][j]; if (i == 0 && j >= 0 && j <= arr[i].length) { eachSum1 += arr[i][j]; } else if (i == 1 && j >= 0 && j <= arr[i].length) { eachSum2 += arr[i][j]; } else if (i == 2 && j >= 0 && j <= arr[i].length) { eachSum3 += arr[i][j]; } else if (i == 3 && j >= 0 && j <= arr[i].length) { eachSum4 += arr[i][j]; } } } System.out.println("第一季度营业额:" + eachSum1); System.out.println("第二季度营业额:" + eachSum2); System.out.println("第三季度营业额:" + eachSum3); System.out.println("第四季度营业额:" + eachSum4); System.out.println("总营业额:" + totalSum); } }