Repository navigation
Expand file tree
/
Copy pathTask2_2_15.java
More file actions
74 lines (62 loc) · 2.53 KB
/
Copy pathTask2_2_15.java
File metadata and controls
74 lines (62 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package intro_to_java.practice_02;
// Найдите наибольший элемент матрицы и замените все нечетные элементы на него.
import java.util.Scanner;
public class Task2_2_15 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Введите количество строк матрицы: ");
int line = scanner.nextInt();
System.out.print("Введите количество столбцов матрицы: ");
int column = scanner.nextInt();
Matrix matrix = new Matrix(line, column);
System.out.println();
System.out.println("Сформированая матрица: ");
matrix.showMatrix();
System.out.println();
System.out.print("Заменяем нечётный элемент на максимальное значение матрицы: ");
matrix.replaceOddEl();
matrix.showMatrix();
}
static class Matrix {
int[][] twoArray;
int[] helpArray;
public Matrix(int line, int column) {
twoArray = new int[line][column];
for (int i = 0; i < twoArray.length; i++) {
for (int j = 0; j < twoArray[i].length; j++) {
twoArray[i][j] = (int) (Math.random() * 100);
if (Math.random() < 0.5) {
twoArray[i][j] *= -1;
}
}
}
helpArray = new int[twoArray.length];
}
public void showMatrix() {
for (int[] arr : twoArray) {
for (int el : arr) {
System.out.print(el + "\t");
}
System.out.println();
}
}
public void replaceOddEl() {
int maxElem = twoArray[0][0];
for (int i = 0; i < twoArray[0].length; i++) {
for (int j = 0; j < twoArray.length; j++) {
if (maxElem < twoArray[i][j]) {
maxElem = twoArray[i][j];
}
}
}
System.out.println(maxElem);
for (int i = 0; i < twoArray[0].length; i++) {
for (int j = 0; j < twoArray.length; j++) {
if (twoArray[i][j] % 2 != 0) {
twoArray[i][j] = maxElem;
}
}
}
}
}
}