Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.32 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class Main {
  3.    
  4.     public static void main(String[] args) {
  5.         // se inicializan variables
  6.         int dimensionX, dimensionY, sumaDiag=0;
  7.        
  8.        
  9.         /*se inicializa objeto scanner
  10.         Con el fin de capturar entradas de
  11.         Teclado
  12.         */
  13.         Scanner leer = new Scanner(System.in) ;
  14.        
  15.         // se inicializan variables
  16.        
  17.         System.out.println("Ingrese dimension X:");
  18.         dimensionX = leer.nextInt();
  19.        
  20.         System.out.println("Ingrese dimension Y:");
  21.         dimensionY = leer.nextInt();
  22.        
  23.         // se declara e inicializa el arreglo
  24.        
  25.         int[] [] arreglo = new int[dimensionX][dimensionY];
  26.         // se procede a poblar la matriz
  27.        
  28.         for(byte x=0;x<dimensionX;x++){
  29.             for(byte y=0;y<dimensionY;y++){
  30.             System.out.println("Ingrese dato para la coordenada ["+x+","+y+"]");
  31.             arreglo[x][y] = leer.nextInt();
  32.         }
  33.         }
  34.        
  35.         // Se calcula la diagonal principal
  36.        
  37.         for(byte diag=0;diag<dimensionX;diag++){
  38.             sumaDiag = sumaDiag + arreglo[diag][diag];
  39.         }
  40.        
  41.         // Se muestra el resultado
  42.        
  43.         System.out.println("La suma de la diagonal es: "+sumaDiag);
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement