Problem with Matrix

0

You will see I am doing a program in which you ask for the elements of a matrix and it returns them to me, the point is that when you execute it, you return the last value that you enter repeatedly. Example if I assign a 2x2 matrix it asks me for the 4 values but only returns the last value repeated 4 times, I have been trying to correct it but I have not succeeded. I know the code is missing some things to make it look good.

public class Suma{
private int[][] arreglo;
private int[][] arreglo1;
private int elemento;
private int elemento1;

public Suma(int elemento, int elemento1){
arreglo = new int[elemento][elemento1];
arreglo1 = new int[elemento][elemento1];
}


public boolean setElemento(int valor){
boolean respuesta = false;
for(int x=0; x<arreglo.length; x++){
    for(int y=0; y<arreglo[x].length; y++){
arreglo[x][y] = valor;
respuesta = true;
  }
 }return respuesta;
}



public boolean setElemento1(int valor1){
boolean respuesta1 = false;
for(int x=0; x<arreglo1.length; x++){
    for(int y=0; y<arreglo1[x].length; y++){
arreglo1[x][y] = valor1;
respuesta1 = true;
  }
 }return respuesta1;
}

public String stoString(){
String elementos = " ";
String elementos1 = " ";
for(int x=0; x<arreglo.length; x++){
    for(int y=0; y<arreglo[x].length; y++){
elementos = elementos + " " + arreglo1[x][y];

}
System.out.println("\n ");
}
return elementos;
}


public String toString(){
String elementos = " ";
String elementos1 = " ";
for(int x=0; x<arreglo.length; x++){
    for(int y=0; y<arreglo[x].length; y++){
elementos = elementos + " " + arreglo[x][y];
}
}
return elementos;
}

}
import java.util.Scanner;

public class Sumas{
public static void main(String []args){

Suma Sumas;
int nfilas, ncolumnas;
int numero, numero1;

Scanner teclado = new Scanner(System.in);
System.out.println("Dame el numero de filas: ");
nfilas = teclado.nextInt();
System.out.println("Dame el numero de columnas: ");
ncolumnas = teclado.nextInt();
Sumas = new Suma(nfilas, ncolumnas);



for(int x=0; x<nfilas; x++){
    for(int y=0; y<ncolumnas; y++){
    System.out.println("Dame el " + x +","+ y + "valor de la primera matriz");
    numero = teclado.nextInt();
    Sumas.setElemento(numero);
    }
}



for(int x=0; x<nfilas; x++){
    for(int y=0; y<ncolumnas; y++){
    System.out.println("Dame el " + x +","+ y + "valor de la segunada matriz");
    numero1 = teclado.nextInt();
    Sumas.setElemento1(numero1);
    }
}



System.out.println("Los elementos ingresados son: " + Sumas.toString());
    
asked by Christian 10.12.2017 в 18:15
source

1 answer

1

first of all you have a programming logic error when adding the elements to the matrices. With your logic the last element entered is always added, in all the fields of the matrix.

public boolean setElemento(int valor){
    boolean respuesta = false;
    //con el doble for se adiciona el contenido de valor en toda la matriz, cosa que no se quiere
    for(int x=0; x<arreglo.length; x++){
        for(int y=0; y<arreglo[x].length; y++){
            arreglo[x][y] = valor;
            respuesta = true;
        }
    }
    return respuesta;
}

The solution is to send the position where you want to save the value sent to setElement like this:

In the Sum class:

for (int x = 0; x < nfilas; x++) {
    for (int y = 0; y < ncolumnas; y++) {
        System.out.println("Dame el " + x + "," + y + "valor de la primera matriz");
        numero = teclado.nextInt();
        Sumas.setElemento(numero, x, y);
    }
}

In the Suma class:

public boolean setElemento(int valor, int x, int y) {
    arreglo[x][y] = valor;
    return true;
}

Clearly, you have to perform the same procedure for the second vector.

    
answered by 11.12.2017 в 06:53