"Program 38"
Title: Finding the sum of diagonal elements of a 3*3 matrix
1. Program Analysis: Use the dual for loop control input two-dimensional array, and then add a[i][i] after the output.
2. Program Source code:
1 Importjava.util.Arrays;2 ImportJava.util.Random;3 4 /**5 *6 * "program 38"7 * Title: Find the sum of the diagonal elements of a 3*3 matrix. 8 * Program Analysis: Use dual for loop control input two-dimensional array, and then add arr[i][i] to output. 9 * Ten * @authorwww.cnblogs.com/java-100 One * A */ - - Public classJava38 { the - Public Static voidMain (string[] args) { - int[] arr =New int[3] [3]; - intsum = 0; +Random random =NewRandom (); - for(inti = 0; i < arr.length; i++) { + for(intj = 0; J < Arr[i].length; J + +) { AARR[I][J] = random.nextint (100); atSystem.out.print (Arr[i][j] + ""); - if(i = = 0 && (j = = 0 | | j = 2) | | i = = 1 && j = = I | | i = 2 && (j = = 0 | | j = 2)) { -Sum + =Arr[i][j]; - } - } - System.out.println (); in } - System.out.println (sum); to } + -}
Java rewrite "C Classic 100 Questions"--38