Advertisement
Guest User

Matriz

a guest
Mar 23rd, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. package course;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Matriz {
  6.  
  7. public static void main(String[] args) {
  8.  
  9. Scanner sc = new Scanner(System.in);
  10. System.out.print("Linhas: ");
  11. int l = sc.nextInt();
  12. System.out.print("Colunas: ");
  13. int c = sc.nextInt();
  14. int mat[][] = new int[l][c];
  15.  
  16. for (int i = 0; i < mat.length; i++) {
  17. for (int j = 0; j < mat[i].length; j++) {
  18. System.out.print("Linha [" + i + "] Coluna [" + j +"]: ");
  19. mat[i][j] = sc.nextInt();
  20. }
  21. }
  22.  
  23. System.out.println("Matriz: ");
  24. for (int i = 0; i < mat.length; i++) {
  25. for (int j = 0; j < mat[i].length; j++) {
  26. System.out.print(" [");
  27. System.out.print(mat[i][j] + "] ");
  28. }
  29. System.out.println();
  30. }
  31.  
  32. System.out.println("Digite um número para buscar a posição: ");
  33. int buscar = sc.nextInt();
  34.  
  35. for (int i = 0; i < mat.length; i++) {
  36. for (int j = 0; j < mat[i].length; j++) {
  37. if(mat[i][j] == buscar) {
  38. System.out.println("O número " + buscar + " está na linha [" + i + "] Coluna [" + j +"].");
  39. if (j > 0) {
  40. System.out.println("Esquerda: " + mat[i][j-1]);
  41. }
  42. if (i > 0) {
  43. System.out.println("Acima: " + mat[i-1][j]);
  44. }
  45. if (j < mat[i].length-1) {
  46. System.out.println("Direita: " + mat[i][j+1]);
  47. }
  48. if (i < mat.length-1) {
  49. System.out.println("Abaixo: " + mat[i+1][j]);
  50. }
  51. }
  52. }
  53. System.out.println();
  54. }
  55.  
  56. sc.close();
  57. }
  58.  
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement