-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFillTheMatrix.java
62 lines (53 loc) · 1.81 KB
/
FillTheMatrix.java
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
package Matrices_Exercises;
import java.util.Scanner;
public class FillTheMatrix {
public static void main(String[] args) {
Scanner scan = new Scanner (System.in);
String[] input = scan.nextLine().split(", ");
int n = Integer.parseInt(input[0]);
String symbol = input[1];
int[][] matrix = new int[n][n];
switch (symbol){
case "A":
matrixOfCaseA(matrix);
break;
case "B":
matrixOfCaseB(matrix);
break;
}
printMatrix(matrix);
}
private static void printMatrix(int[][] matrix) {
for (int row = 0; row < matrix.length; row++) {
for (int col = 0; col < matrix.length; col++) {
System.out.print(matrix[row][col] + " ");
}
System.out.println();
}
}
private static void matrixOfCaseB(int[][] matrix) {
int startingNumber = 1;
for (int col = 0; col < matrix.length; col++) {
if(col % 2 == 0){
for (int row = 0; row < matrix.length; row++) {
matrix[row][col] = startingNumber;
startingNumber++;
}
}else{
for (int row = matrix.length -1; row >= 0 ; row--) {
matrix[row][col] = startingNumber;
startingNumber++;
}
}
}
}
private static void matrixOfCaseA(int[][] matrix) {
int startingNumber = 1;
for (int col = 0; col < matrix.length; col++) {
for (int row = 0; row < matrix.length; row++) {
matrix[row][col] = startingNumber;
startingNumber++;
}
}
}
}