Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package course;
- import java.util.Scanner;
- public class Matriz {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- System.out.print("Linhas: ");
- int l = sc.nextInt();
- System.out.print("Colunas: ");
- int c = sc.nextInt();
- int mat[][] = new int[l][c];
- for (int i = 0; i < mat.length; i++) {
- for (int j = 0; j < mat[i].length; j++) {
- System.out.print("Linha [" + i + "] Coluna [" + j +"]: ");
- mat[i][j] = sc.nextInt();
- }
- }
- System.out.println("Matriz: ");
- for (int i = 0; i < mat.length; i++) {
- for (int j = 0; j < mat[i].length; j++) {
- System.out.print(" [");
- System.out.print(mat[i][j] + "] ");
- }
- System.out.println();
- }
- System.out.println("Digite um número para buscar a posição: ");
- int buscar = sc.nextInt();
- for (int i = 0; i < mat.length; i++) {
- for (int j = 0; j < mat[i].length; j++) {
- if(mat[i][j] == buscar) {
- System.out.println("O número " + buscar + " está na linha [" + i + "] Coluna [" + j +"].");
- if (j > 0) {
- System.out.println("Esquerda: " + mat[i][j-1]);
- }
- if (i > 0) {
- System.out.println("Acima: " + mat[i-1][j]);
- }
- if (j < mat[i].length-1) {
- System.out.println("Direita: " + mat[i][j+1]);
- }
- if (i < mat.length-1) {
- System.out.println("Abaixo: " + mat[i+1][j]);
- }
- }
- }
- System.out.println();
- }
- sc.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement