-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrix.java
57 lines (47 loc) · 1.42 KB
/
Matrix.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
import java.util.Scanner;
public class Matrix {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
System.out.print("Matrix's rows number: ");
int m = sc.nextInt();
System.out.print("Matrix's columens number: ");
int n = sc.nextInt();
int[][] matriz = new int[m][n];
for(int i=0; i<m; i++){
for(int j=0; j<n; j++){
matriz[i][j] = sc.nextInt();
}
}
System.out.println("----- Matrix -----");
for(int i=0; i<m; i++){
for(int j=0; j<n; j++){
System.out.print(matriz[i][j] + " ");
}
System.out.println();
}
System.out.println();
System.out.print("What number do you want to search for? ");
int x = sc.nextInt();
System.out.println();
for (int i=0; i<matriz.length; i++) {
for (int j=0; j<matriz[i].length; j++) {
if (matriz[i][j] == x) {
System.out.println("Position [" + i + "][" + j + "]");
if (j > 0) {
System.out.println("Left: " + matriz[i][j-1]);
}
if (i > 0) {
System.out.println("Up: " + matriz[i-1][j]);
}
if (j < matriz[i].length-1) {
System.out.println("Right: " + matriz[i][j+1]);
}
if (i < matriz.length-1) {
System.out.println("Down: " + matriz[i+1][j]);
}
}
}
}
sc.close();
}
}