Matrices en Java
Publicado por Smichael (1 intervención) el 13/08/2015 22:49:06
Hola, necesito una Matriz N*N que genera valor al azar, que los valores no se repitan, que permita buscar un valor de la matriz que me indique la posición y que finalmente me organice la matriz en orden. Apenas estoy comenzando y no he podido dar con la solución.
tengo esto//
tengo esto//
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package apppractica1tp;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
public class AppPractica1TP {
private static BufferedReader br;
public static void main(String[] args) {
br = new BufferedReader(new InputStreamReader(System.in));
int cantFilas = 0, cantCols = 0;
int mat[][];
try {
System.out.println("Filas");
cantFilas = Integer.parseInt(br.readLine());
System.out.println("Columnas");
cantCols = Integer.parseInt(br.readLine());
} catch (IOException ex) {
System.out.println("Ingrese valores numericos");
}
Matriz matriz = new Matriz(cantFilas, cantCols);
matriz.getMat();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package apppractica1tp;
import java.util.Random;
public class Matriz {
//1. Propiedades
private int mat[][];
private int cantFilas;
private int cantCols;
//2. Método Constructor
public Matriz(int cantFilas, int cantCols) {
this.cantFilas = cantFilas;
this.cantCols = cantCols;
llenarMatriz();
}
//3. Métodos Set y Get
public void setCantCols(int cantCols) {
this.cantCols = cantCols;
}
public void setCantFilas(int cantFilas) {
this.cantFilas = cantFilas;
}
public void setMat(int[][] mat) {
this.mat = mat;
}
public void getMat() {
for (int i = 0; i < cantFilas; i++) {
for (int j = 0; j < cantCols; j++) {
System.out.println(mat[i][j] + "\t");
}
System.out.println("");
}
}
//4. Demás métodos
public boolean buscarValor(int dato, int opc){
boolean b = true;
//......
return b;
}
public void odenarMatriz(){
//...
getMat();
}
private void llenarMatriz(){
mat = new int[cantFilas][cantCols];
Random r = new Random();
for (int i = 0; i < cantFilas; i++) {
for (int j = 0; j < cantCols; j++) {
mat[i][j] = r.nextInt(cantFilas*cantCols);
}
}
}
}
Valora esta pregunta


0