Home >Java >javaTutorial >Java example: Calculation of the sum of the two diagonal numbers of the 6x6 grid (code)
The content of this article is about Java examples: calculation (code) of the sum of the two diagonal numbers of the 6x6 grid. It has certain reference value. Friends in need can refer to it. I hope it will be useful to you. Helps.
Calculate the sum of the two diagonal numbers in the figure, and wrote a relatively simple code
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); } }
Related recommendations:
Convert xml to Bean instance parsing in java (pure code)
Creation of Java objects: Initialization of classes Timing and process
The above is the detailed content of Java example: Calculation of the sum of the two diagonal numbers of the 6x6 grid (code). For more information, please follow other related articles on the PHP Chinese website!