Created
October 14, 2017 08:30
-
-
Save Sadwyn/44dcb9cb5e0601a122eded4420f3d548 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.company; | |
import java.util.Random; | |
public class Main { | |
public static void main(String[] args) { | |
int sum; | |
int[][] matrix = new int[3][3]; | |
Random random = new Random(); | |
initArray(matrix, random); | |
printArray(matrix); | |
sum = getSum(matrix); | |
System.out.println(String.valueOf("Сумма элементов под и над главной диагональю = " + sum)); | |
} | |
private static void initArray(int[][] matrix, Random random) { | |
for (int i = 0; i < 3; i++) { | |
for (int j = 0; j < 3; j++) { | |
matrix[i][j] = random.nextInt(10); | |
} | |
} | |
} | |
private static void printArray(int[][] matrix) { | |
for (int i = 0; i < 3; i++) { | |
for (int j = 0; j < 3; j++) { | |
System.out.print(String.valueOf(matrix[i][j]) + " "); | |
} | |
System.out.println(); | |
} | |
} | |
private static int getSum(int[][] matrix) { | |
int sum = 0; | |
for (int i = 0; i < 3; i++) { | |
for (int j = 0; j < 3; j++) { | |
if (i != j) | |
sum += matrix[i][j]; | |
} | |
} | |
return sum; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment