简体   繁体   中英

How can I set dimension size of a matrix and then assign values to that matrix from other methods?

I have this code that actually works! But it's not exception safe (if I write any char, will crash) , So I'm trying to make it better.

As you can see in the code down there, I'm initializing the variable fila and columna, asking how many rows(filas) and columns (columnas) is going to have the matrix, next, I initialize the matrix using the two previous variables.

By using this approach, I can't use Try & Catch, but I have no idea how else can I initialize the matrix with user input and being able to use those variables in my others methods.


Working Code

import javax.swing.JOptionPane;

public class test {

int i = 0;
int j = 0;
int fila = Integer.parseInt(JOptionPane.showInputDialog(null, "Cuántas filas?", "Iniciando", JOptionPane.QUESTION_MESSAGE));
int columna = Integer.parseInt(JOptionPane.showInputDialog(null, "Cuántas columnas?", "Iniciando", JOptionPane.QUESTION_MESSAGE));
int [][] matrix = new int[fila][columna];
int valorCentral = columna/2;
int valorDiagonal = 0;
boolean error = false;
StringBuffer elementos = new StringBuffer();
StringBuffer elementosNullificados = new StringBuffer();
StringBuffer elementosEsquineros = new StringBuffer();
StringBuffer elementoUnoCentrado = new StringBuffer();
StringBuffer elementoUnoEnDiagonal = new StringBuffer();

public static void main(String[] args) {
    test test = new test();
    test.pideDatos();;
    test.llenado();
    test.nullificador();
}

public void pideDatos(){

}

public void llenado()
    {
           for (int i = 0; i < fila; i++)  // Llenado de la matriz
           {         
               for (int j = 0; j < columna; j++)
               {    
                 matrix[i][j] = Integer.parseInt(JOptionPane.showInputDialog(null, "Fila [" + (i+1) + "]:  Columna [" + (j+1) + "]", "Iniciando", JOptionPane.QUESTION_MESSAGE));
                 elementos.append("["+matrix[i][j]+"] ");  // Agrega cada fila de la matriz a value.
                   if(j == columna-1) // Cuando se llegue a la última columna se le agrega un \n
                   {
                       elementos.append("\n                 ");
                   }               
               }
            }

           elementos.toString(); // Convierte a String los elementos en value
           JOptionPane.showMessageDialog(null, "Elementos de la matriz "+fila+"x"+columna+":\n\n                 "+elementos, "Elementos", JOptionPane.INFORMATION_MESSAGE, null);

    }

    public void nullificador(){

        for (int i = 0; i < fila; i++)  // Llenado de la matriz
           {         
               for (int j = 0; j < columna; j++)
               {
                 matrix[i][j] = 0;
                 elementosNullificados.append("["+matrix[i][j]+"] ");  // Agrega cada fila de la matriz a value.
                   if(j == columna-1) // Cuando se llegue a la última columna se le agrega un \n
                   {
                       elementosNullificados.append("\n                   ");
                   }               
               }
            }
            elementosNullificados.toString(); // Convierte a String los elementos en value
           JOptionPane.showMessageDialog(null, "Elementos nullificados de la matriz "+fila+"x"+columna+":\n\n                   "+elementosNullificados, "Elementos", JOptionPane.INFORMATION_MESSAGE, null);


    }

What I've tried so far its to intilialize the matrix in a method, but how can I use those variables in the others methods?


import javax.swing.JOptionPane;

public class test {

int i = 0;
int j = 0;

boolean error = false;
StringBuffer elementos = new StringBuffer();
StringBuffer elementosNullificados = new StringBuffer();
StringBuffer elementosEsquineros = new StringBuffer();
StringBuffer elementoUnoCentrado = new StringBuffer();
StringBuffer elementoUnoEnDiagonal = new StringBuffer();

public static void main(String[] args) {
    test test = new test();
    test.pideDatos();;
    test.llenado();
    test.nullificador();
}

public void pideDatos(){

    int fila = Integer.parseInt(JOptionPane.showInputDialog(null, "Cuántas filas?", "Iniciando", JOptionPane.QUESTION_MESSAGE));
    int columna = Integer.parseInt(JOptionPane.showInputDialog(null, "Cuántas columnas?", "Iniciando", JOptionPane.QUESTION_MESSAGE));
    int [][] matrix = new int[fila][columna];
    int valorCentral = columna/2;
    int valorDiagonal = 0;
}

public void llenado()
    {
           for (int i = 0; i < fila; i++)  // Llenado de la matriz
           {         
               for (int j = 0; j < columna; j++)
               {    
                 matrix[i][j] = Integer.parseInt(JOptionPane.showInputDialog(null, "Fila [" + (i+1) + "]:  Columna [" + (j+1) + "]", "Iniciando", JOptionPane.QUESTION_MESSAGE));
                 elementos.append("["+matrix[i][j]+"] ");  // Agrega cada fila de la matriz a value.
                   if(j == columna-1) // Cuando se llegue a la última columna se le agrega un \n
                   {
                       elementos.append("\n                 ");
                   }               
               }
            }

           elementos.toString(); // Convierte a String los elementos en value
           JOptionPane.showMessageDialog(null, "Elementos de la matriz "+fila+"x"+columna+":\n\n                 "+elementos, "Elementos", JOptionPane.INFORMATION_MESSAGE, null);

    }
public void nullificador(){

    for (int i = 0; i < fila; i++)  // Llenado de la matriz
       {         
           for (int j = 0; j < columna; j++)
           {
             matrix[i][j] = 0;
             elementosNullificados.append("["+matrix[i][j]+"] ");  // Agrega cada fila de la matriz a value.
               if(j == columna-1) // Cuando se llegue a la última columna se le agrega un \n
               {
                   elementosNullificados.append("\n                   ");
               }               
           }
        }
        elementosNullificados.toString(); // Convierte a String los elementos en value
       JOptionPane.showMessageDialog(null, "Elementos nullificados de la matriz "+fila+"x"+columna+":\n\n                   "+elementosNullificados, "Elementos", JOptionPane.INFORMATION_MESSAGE, null);


}

You can define a null Array to start with then initialize it in a later function, this will allow other methods to get the information outside of the method that Initialized it.

public class test {
{
  private int[][] matrix;

  public void pideDatos() {
    int fila = Integer.parseInt(JOptionPane.showInputDialog(null, "Cuántas filas?", "Iniciando", JOptionPane.QUESTION_MESSAGE));
    int columna = Integer.parseInt(JOptionPane.showInputDialog(null, "Cuántas columnas?", "Iniciando", JOptionPane.QUESTION_MESSAGE));
    matrix = new int[fila][columna];
  }
}

Give that a crack

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM