1

me encargaron realizar un programa para declarar y hacer 2 matrices, matriz1 y matriz2, y hacer un ciclo para leer enteros y guardalos en matriz1 lo cual creo que ya hice, despues hacer otro ciclo que analize los numeros de matriz1 y si es par, multiplicarlo por 2 y pasarlo a matriz2 y si es impar multiplicarlo por 3 y de nuevo pasarlo a matriz2 y luego mostrar las 2 matrices.

ejemplo:

introducir la descripción de la imagen aquí

esto es lo que llevo hasta el momento.

        Scanner lector = new Scanner(System.in);
        System.out.print(" numero de renglones " );
        int ren = Integer.parseInt(lector.nextLine());        
        System.out.print(" numero de columnas ");
        int col = Integer.parseInt(lector.nextLine());
        int [][] matrizent1 = new int[ren][col];
        int [][] matrizent2 = new int[col][ren];
        for (int i = 0; i < matrizent1.length; i++)
        {
            for(int j = 0; j < matrizent1[i].length; j++)
            {
                System.out.print(" valor de la posicion " + i + ", " + j + ": ");
                matrizent1[i][j] = Integer.parseInt(lector.nextLine());
            }
        }
        for (int i = 0; i < matrizent1.length; i++)
        {   
           for (int j = 0; j < matrizent1[i].length; j++)
           {
               matrizent2[j][i] = matrizent1[i][j];
           }
        }
        System.out.println(" matrizent1 ");
        for (int i = 0; i < matrizent1.length; i++)
        {
            for (int j = 0; j < matrizent1[i].length; j++)
            {
                System.out.print(matrizent1[i][j] + "\t");
            }
            System.out.println();
        }

se agradece mucho la ayuda.

1
  • Tu código como texto por favor
    – BetaM
    el 9 may. 2020 a las 19:21

1 respuesta 1

1

Creo que la siguiente modificación en tu segundo recorrido puede lograr tu objetivo.

for(int i = 0; i < matrizent1.length ; i++){
    for(int j = 0; j < matrizent1[i].length ; j++){
        if(matrizent1[i][j] % 2 == 0){
           matrizent2[i][j] = matrizent1[i][j]*2;
        }else{
           matrizent2[i][j] = matrizent1[i][j]*3;
        }
    }
}

Lo que hace matrizent[i][j]%2 es evaluar si el resto de dividir el número en la Zelda matrizent[i][j] es cero o no. Si es cero entonces significa que el número es par, caso contrario es impar. Espero te ayude :)

1
  • si había pensado en algo parecido pero no sabia si funcionaria, te agradezco mucho por tu ayuda rguzman :) el 9 may. 2020 a las 19:38

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.