Repository navigation
Expand file tree
/
Copy pathTask2_2_8.java
More file actions
67 lines (55 loc) · 2.62 KB
/
Copy pathTask2_2_8.java
File metadata and controls
67 lines (55 loc) · 2.62 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
package intro_to_java.practice_02;
// В числовой матрице поменять местами два столбца любых столбца, т.е все элементы одного
// столбца поставить на соответствующие им позиции другого, а его элементы второго
// переместить в первый. Номера столбцов вводит пользователь с клавиатуры.
import java.util.Scanner;
public class Task2_2_8 {
public static void main(String[] args) {
Matrix matrix = new Matrix();
Scanner scanner = new Scanner(System.in);
System.out.print("Введите количество строк матрицы: ");
int line = scanner.nextInt();
System.out.print("Введите количество столбцов матрицы: ");
int column = scanner.nextInt();
matrix.createMatrix(line, column);
System.out.println();
System.out.println("Первоначальная матрица: ");
matrix.showMatrix();
System.out.println();
System.out.print("Введите номер первого столбца для замены: ");
int first = scanner.nextInt();
System.out.print("Введите номер второго столбца для замены: ");
int second = scanner.nextInt();
System.out.println();
matrix.shuffleMatrix(first, second);
System.out.println("Изменённая матрица");
matrix.showMatrix();
}
static class Matrix {
int[][] twoArray;
public void createMatrix(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);
}
}
}
public void showMatrix() {
for (int[] arr : twoArray) {
for (int el : arr) {
System.out.print(el + "\t");
}
System.out.println();
}
}
public void shuffleMatrix(int first, int second) {
int[] helpArray = new int[twoArray.length];
for (int i = 0; i < twoArray.length; i++) {
helpArray[i] = twoArray[i][first];
twoArray[i][first] = twoArray[i][second];
twoArray[i][second] = helpArray[i];
}
}
}
}