Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- // se inicializan variables
- int dimensionX, dimensionY, sumaDiag=0;
- /*se inicializa objeto scanner
- Con el fin de capturar entradas de
- Teclado
- */
- Scanner leer = new Scanner(System.in) ;
- // se inicializan variables
- System.out.println("Ingrese dimension X:");
- dimensionX = leer.nextInt();
- System.out.println("Ingrese dimension Y:");
- dimensionY = leer.nextInt();
- // se declara e inicializa el arreglo
- int[] [] arreglo = new int[dimensionX][dimensionY];
- // se procede a poblar la matriz
- for(byte x=0;x<dimensionX;x++){
- for(byte y=0;y<dimensionY;y++){
- System.out.println("Ingrese dato para la coordenada ["+x+","+y+"]");
- arreglo[x][y] = leer.nextInt();
- }
- }
- // Se calcula la diagonal principal
- for(byte diag=0;diag<dimensionX;diag++){
- sumaDiag = sumaDiag + arreglo[diag][diag];
- }
- // Se muestra el resultado
- System.out.println("La suma de la diagonal es: "+sumaDiag);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement