Java - Problema con matrices en java

 
Vista:

Problema con matrices en java

Publicado por Juan Sebastian Cubilos Gonzale (1 intervención) el 26/01/2011 00:43:14
Hola, soy un estudiante de java, lo manejo por netbeens, estoy empezando hacer matrices, y he tenido bastante probelmas con ellas, pues no he entendido bien su teoria, soy nuevo en el mundo de la programacion, y aqui va la duda, como hago para hacer una matriz que sea digite por el usuario y no designada por mi, creo que con el archivo leer, leer,dato(); funciona pero tengo mis dudas ahora esta matriz no puede ser par debe ser impar, para validar este acceso debo colocar una restriccion el punto es como, gracias...?


package parqueadero;

/**
*
* @author Sbastian
*/
public class matriz {
public static void main (String [] args) {
// System.out.println("Porfavor diggite el tamaño de su matriz");
// System.out.println(Leer.dato());
int matriz [][] = new int [3][3];
for (int i=0 ; i <=2; i++)
{
for (int j=0; j<= 2 ; j++)

{
matriz [i][j]= i + 1 ;
}


}
System.out.println("\n Matriz de 3 * 3");
for (int i=0 ; i <=2; i++)
{
for (int j=0; i<= 2 ; i++)
System.out.print(matriz [i][j] + "\t" );
System.out.println();
}
}
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

RE:Problema con matrices en java

Publicado por aitor (85 intervenciones) el 26/01/2011 12:36:45
public static void main(String[] args) {
Scanner leer = new Scanner(System.in);
int dato = 0;

do {
System.out.println("Porfavor diggite el tamaño de su matriz");
dato = leer.nextInt();
} while (dato % 2 == 0);

int matriz[][] = new int[dato][dato];
for (int i = 0; i < matriz.length; i++) {
for (int j = 0; j < matriz[i].length; j++) {
matriz[i][j] = i + 1;
}

}
System.out.println("\n Matriz de "+dato+" * "+dato);
for (int i = 0; i <matriz.length; i++) {
for (int j = 0; j <matriz[i].length; j++) {
System.out.print(matriz[i][j] + "\t");
}
System.out.println();
}
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar