-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrizExercicio.java
More file actions
56 lines (45 loc) · 1.13 KB
/
MatrizExercicio.java
File metadata and controls
56 lines (45 loc) · 1.13 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
package entities;
import java.util.Scanner;
public class MatrizExercicio {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("M:");
int m = sc.nextInt();
System.out.println("N:");
int n = sc.nextInt();
int mat[][] = new int[m][n];
for(int i = 0; i < m ; i++) {
for(int j = 0; j < n ; j++) {
mat[i][j] = sc.nextInt();
}
}
System.out.println("Choose a number: ");
int numEscolhido = sc.nextInt();
boolean found = false;
for(int i = 0 ; i<m ; i++) {
for(int j = 0 ; j<n ; j++) {
if(numEscolhido == mat[i][j]) {
System.out.println("Position: " + i + ", " + j);
if(j > 0) {
System.out.println("Left: " + mat[i][j-1]);
}
if(j < n-1) {
System.out.println("Right: " + mat[i][j+1]);
}
if(i > 0) {
System.out.println("Left: " + mat[i-1][j]);
}
if(i < m-1) {
System.out.println("Left: " + mat[i+1][j]);
}
found = true;
System.out.println(" ");
}
}
}
if(!found) {
System.out.println("The number was not found!");
}
sc.close();
}
}