Caso exista uma sequencia de (neste caso) tres valores iguais nas linhas ou colunas ou diagonais voce deve informar onde isso esta acontecendo.
Matriz Original
…123
1 xxx
2 xxx
3 xxx
Exemplo1
1 2 3
1 1 3
1 2 1
Coluna 1 aparece o valor 1
Diagonal principal aparece o valor 1
Exemplo2
1 2 3
1 2 3
1 2 3
Coluna 1 aparece o valor 1
Coluna 2 aparece o valor 2
Coluna 3 aparece o valor 3
Exemplo3
1 1 1
1 1 1
1 1 1
Coluna 1 aparece o valor 1
Coluna 2 aparece o valor 1
Coluna 3 aparece o valor 1
Linha 1 aparece o 1
Linha 2 aparece o 1
Linha 3 aparece o 1
Diagonal principal aparece 1
Diagonal secundaria aparece o 1
Se for exatamente isso uma possivel solução é:
for (int i = 0; i < matriz.length; i++) {
for (int j = 0; j < matriz[0].length; j++) {
// diagonal principal
if (i == j) {
if (matriz[i][j] == 1) {
d11++;
} else if (matriz[i][j] == 2) {
d12++;
} else if (matriz[i][j] == 3) {
d13++;
}
}
// diagonal secundaria
if (i + j == matriz.length - 1) {
if (matriz[i][j] == 1) {
d21++;
} else if (matriz[i][j] == 2) {
d22++;
} else if (matriz[i][j] == 3) {
d23++;
}
}
// 1ª linha
if (i == 0) {
if (matriz[i][j] == 1) {
l11++;
} else if (matriz[i][j] == 2) {
l12++;
} else if (matriz[i][j] == 3) {
l13++;
}
}
// 2ª linha
if (i == 1) {
if (matriz[i][j] == 1) {
l21++;
} else if (matriz[i][j] == 2) {
l22++;
} else if (matriz[i][j] == 3) {
l23++;
}
}
// 3ª linha
if (i == 2) {
if (matriz[i][j] == 1) {
l31++;
} else if (matriz[i][j] == 2) {
l32++;
} else if (matriz[i][j] == 3) {
l33++;
}
}
// 1ª coluna
if (j == 0) {
if (matriz[i][j] == 1) {
c11++;
} else if (matriz[i][j] == 2) {
c12++;
} else if (matriz[i][j] == 3) {
c13++;
}
}
// 2ª coluna
if (j == 1) {
if (matriz[i][j] == 1) {
c21++;
} else if (matriz[i][j] == 2) {
c22++;
} else if (matriz[i][j] == 3) {
c23++;
}
}
// 3ª coluna
if (j == 2) {
if (matriz[i][j] == 1) {
c31++;
} else if (matriz[i][j] == 2) {
c32++;
} else if (matriz[i][j] == 3) {
c33++;
}
}
}// for
}// for
// LINHAS
if (l11 < 3 && l21 < 3 && l31 < 3 && l12 < 3 && l22 < 3 && l32 < 3
&& l13 < 3 && l23 < 3 && l33 < 3) {
// Em nenhuma linha os valores se repetem
} else {
if (l11 >= 3) {
System.out.println("Linha 1 aparece o valor 1");
} else if (l12 >= 3) {
System.out.println("Linha 1 aparece o valor 2");
} else if (l13 >= 3) {
System.out.println("Linha 1 aparece o valor 3");
}
if (l21 >= 3) {
System.out.println("Linha 2 aparece o valor 1");
} else if (l22 >= 3) {
System.out.println("Linha 2 aparece o valor 2");
} else if (l23 >= 3) {
System.out.println("Linha 2 aparece o valor 3");
}
if (l31 >= 3) {
System.out.println("Linha 3 aparece o valor 1");
} else if (l32 >= 3) {
System.out.println("Linha 3 aparece o valor 2");
} else if (l33 >= 3) {
System.out.println("Linha 3 aparece o valor 3");
}
}
// COLUNAS
if (c11 < 3 && c21 < 3 && c31 < 3 && c12 < 3 && c22 < 3 && c32 < 3
&& c13 < 3 && c23 < 3 && c33 < 3) {
// Em nenhuma coluna os valores se repetem
} else {
if (c11 >= 3) {
System.out.println("Coluna 1 aparece o valor 1");
} else if (c12 >= 3) {
System.out.println("Coluna 1 aparece o valor 2");
} else if (c13 >= 3) {
System.out.println("Coluna 1 aparece o valor 3");
}
if (c21 >= 3) {
System.out.println("Coluna 2 aparece o valor 1");
} else if (c22 >= 3) {
System.out.println("Coluna 2 aparece o valor 2");
} else if (c23 >= 3) {
System.out.println("Coluna 2 aparece o valor 3");
}
if (c31 >= 3) {
System.out.println("Coluna 3 aparece o valor 1");
} else if (c32 >= 3) {
System.out.println("Coluna 3 aparece o valor 2");
} else if (c33 >= 3) {
System.out.println("Coluna 3 aparece o valor 3");
}
}
// DIAGONAIS
if (d11 < 3 && d21 < 3 && d12 < 3 && d22 < 3 && d13 < 3 && d23 < 3) {
// Em nenhuma diagonal os valores se repetem
} else {
if (d11 >= 3) {
System.out.println("Diagonal Principal aparece o valor 1");
} else if (d12 >= 3) {
System.out.println("Diagonal Principal aparece o valor 2");
} else if (d13 >= 3) {
System.out.println("Diagonal Principal aparece o valor 3");
}
if (d21 >= 3) {
System.out.println("Diagonal Secundaria aparece o valor 1");
} else if (d22 >= 3) {
System.out.println("Coluna Secundaria aparece o valor 2");
} else if (d23 >= 3) {
System.out.println("Coluna Secundaria aparece o valor 3");
}
}
}// main
Não sei se é possivel, mas gostaria de saber do pessoal do forum se este codigo pode ser implementado para que funcione com matriz de tamanho N, pois este aqui que eu postei nao funciona.
gpd38 num da dessas cara… possivelmente é um trabalho de escola q ele se quer vai ler o q vc fez, vai copiar e colar… e esse não é o objetivo do forum.
Verdade! Mas como o gpd38 ( que realmente tentou fazer e fez! ) e ele perguntou sobre a possibilidade de definir ter uma matriz [n][n], eu vou postar minha sugestão.
P.S: Estou sem JDK instalado nessa máquina então não consegui testar! Tive que fazer tudo no NotePad++ e o teste de mesa, literalmente, aconteceu na mesa! Testem! Qualquer coisa me avisem!
[code]public class Principal{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Informe o tamanho da matriz (linhas)");
int l = in.nextInt();
System.out.println("Informe o tamanho da matriz (colunas)");
int c = in.nextInt();
int[][] matTeste;
Matriz m = new Matriz(l, c);
m.preencherMatriz();
matTeste = m.getMatriz();
for (int x = 0; x < matTeste.length; x++) {
for (int y = 0; y < matTeste[0].length; y++) {
System.out.print(matTeste[x][y]);
}
System.out.println("");
}
System.out.println("-----------------------------------");
m.checkColunas(matTeste);
}
}
[/code]
[code]public class Matriz {
int[][] matrizPreenchida;
public Matriz(int l, int c) {
this.matrizPreenchida = new int[l][c];
}
public void preencherMatriz() {
for (int x = 0; x < this.matrizPreenchida.length; x++) {
for (int y = 0; y < this.matrizPreenchida[0].length; y++) {
this.matrizPreenchida[x][y] = this.getNumeroAleatorio();
}
}
}
public int[][] getMatriz() {
return this.matrizPreenchida;
}
public int getNumeroAleatorio() {
Random objeto = new Random();
int n = 1 + objeto.nextInt(6);
return n;
}
public int[] getVetorAuxiliar(int[][] matriz, int coluna) {
int[] vetorAuxiliar = new int[matriz.length];
for (int x = 0; x < matriz.length; x++) {
//carrega o vetorAuxiliar com os dados da coluna
vetorAuxiliar[x] = matriz[x][coluna];
}
return vetorAuxiliar;
}
public void checkColunas(int[][] matriz) {
int[] vetorAuxiliar;
int varTemp=0;
int qtdRepeticao = 1;
for (int y = 0; y < matriz[0].length; y++) {
vetorAuxiliar = this.getVetorAuxiliar(matriz, y);
varTemp = vetorAuxiliar[0];
for (int i = 1; i < vetorAuxiliar.length; i++) {
if (vetorAuxiliar[i] == varTemp) {
qtdRepeticao++;
} else {
if (qtdRepeticao > 1) {
System.out.println("O valor"+varTemp+" esta repetido na coluna"+y);
qtdRepeticao = 1;
}
varTemp = vetorAuxiliar[i];
}
}
if (qtdRepeticao > 1) {
System.out.println("O valor esta repetido vezes"+varTemp+" por "+qtdRepeticao+" vezes");
}
}
}
Pow cara, valew mesmo.
Sim, isso é um trabalho pra escola. Mas juro que não fiz por preguiça e sim, por falta de lógica. Só precisavam tentar me explicar, mas como vc fez, melhor ainda =]