Java rewrite "C Classic 100 Questions"--38

Source: Internet
Author: User

"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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.