2016-10-13 12 views
0

私は実際に動作するこのコードを持っています!しかしそれは例外的な安全ではありません(私は任意の文字を書く場合、クラッシュする)ので、私はそれをより良くしようとしています。行列の寸法サイズを設定し、その行列に他の方法の値を割り当てるにはどうすればよいですか?

私は変数filaとcolumnaを初期化して、いくつの行(フィラ)と列(columnas)が行列を持つかを尋ねています。次に、次のように使用して行列を初期化します。前の2つの変数。

私はtry &キャッチを使用することはできませんが、私は他の方法でそれらの変数を使用できるように、ユーザー入力でマトリックスをどのように初期化できるか分かりません。


ワーキングコード

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); 


    } 

私はその方法で行列をintilializeし、これまでに試したが、どのように私は他の方法でこれらの変数を使用することができますか?


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); 


} 

答えて

1

あなたは、その後に開始するようにヌル配列を定義することができ、後で機能でそれを初期化し、これは他の方法がそれを初期化する方法の外に情報を取得できるようになります。

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]; 
    } 
} 

は割れていること

+0

感謝を与えます!出来た! –

関連する問題