Package WZS. arithmetics; // question: Calculate the sum of the diagonal elements of a 3*3 matrix. Public class test_wzs26 {public static void main (string [] ARGs) {int array [] [] = {1, 2, 3}, {4, 5, 6}, {7, 8, 9}; system. out. println ("print matrix:"); For (INT I = 0; I <3; I ++) {for (Int J = 0; j <3; j ++) {system. out. print (array [I] [J] + "\ t");} system. out. println ();} system. out. println ("print the right corner element and sum. "); int sum = 0; For (INT I = 0; I <3; I ++) {for (Int J = 0; j <3; j ++) {if (I = J) {sum + = array [I] [J]; system. out. print (array [I] [J] + "\ t") ;}} system. out. println ("Corner element and:" + sum); int sum2 = 0; For (INT I = 0; I <3; I ++) {for (Int J = 0; j <3; j ++) {if (I + J = 2) {sum2 + = array [I] [J]; system. out. print (array [I] [J] + "\ t") ;}} system. out. println ("Corner element and:" + sum2 );}}
Output result:
Print matrix: 123456789 print the corner element and sum. 159 corner elements and: 15357 corner elements and: 15