Obtain odd numbers from a row of a matrix and place the percentage of each row in an array

-1

I have a problem and that is that when I add too many lines

import javax.swing.JOptionPane;
import java.util.Scanner;
public class unm4sk
{
  public static void main(String[] args)
  {
    int array []=new int [5];
    int SFil;
    //TABLA CON DATOS YA INDICADOS EN EL EXAMEN
    int tabla[][] = {
                        { 95, 45, 37, 70, 85 },
                        { -4, 10, 92, 49, 48 },
                        { 2, 30, 51, 100, -9 }
                     };

    for(int i = 0; i<tabla.length; i++)
    {
     for(int j = 0; j<tabla[i].length; j++)
      {
        if (tabla[i][j]%2!=0)
        {
          for (int k=0;k<3 ;k++ )
          {
            SFil=0;
            for (int p=0;p<5 ;p++ )
            {
              SFil=SFil+tabla[i][j];
              System.out.print("\nLa suma de la fila "+k+1+" es: "+SFil);
            }
            System.out.println();
          }
        }
      }
    }
  }
}
    
asked by xero399 12.12.2018 в 11:01
source

1 answer

2

As mentioned in the question, I think what you want is the following:

import javax.swing.JOptionPane;
import java.util.Scanner;
public class unm4sk
{
  public static void main(String[] args)
  {
    int array []=new int[3];
    int SFil;
    //TABLA CON DATOS YA INDICADOS EN EL EXAMEN
    int tabla[][] = {
                        { 95, 45, 37, 70, 85 },
                        { -4, 10, 92, 49, 48 },
                        { 2, 30, 51, 100, -9 }
                     };
    //Recorro las filas
    for(int i = 0; i<tabla.length; i++)
    {
        //Inicializo el contador a 0
        SFil=0;
        //Recorro los elemntos de la fila i
        for(int j = 0; j<tabla[i].length; j++)
        {   
            //Compruebo si el elemento es impar, y en caso de serlo incremento el contador
            if (tabla[i][j]%2!=0)
            {
                SFil++; 
            }
        }
        //Caculo el porcentaje y lo guardo en la posición del array correspondiente a la fila i
        array[i]=(100*SFil)/tabla[i].length;
        System.out.println("\nLa fila "+i+" tiene "+SFil+" números impares ("+array[i]+"%)");
    }
  }
}

I go through each row (index i) and count how many odd elements there are (each element is traversed with the index j and variable increment "SFil" if the element is odd). At the end of the count for each row, I calculate the percentage and store it in the position corresponding to the row in the variable "array" (array [i]) and show it by console.

    
answered by 12.12.2018 / 11:46
source