Java:计算6x6宫格两对角线数字之和
计算如图的两对角线数之和 ,编写了一段较为简捷的代码
public class Training {
public static void main(String[] args) {
int [][] scores = new int[6][6];
int num = 0;
int sum1 = 0;
int sum2 = 0;
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
scores[i][j]=++num;
}
}
for(int i = 0; i< 6;i++){
sum1 += scores[i][i];
sum2 += scores[i][6-i-1];
}
System.out.println(sum1);
System.out.println(sum2);
}
}
输出情况如下: