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