【发布时间】:2016-04-08 01:29:12
【问题描述】:
我已经在一个年级学习了三个月的 Java,现在我正在重做一些我们以前做过的练习,而我正在做一个以前做不到的练习。 措辞是这样的: 编写一个程序,通过扫描仪启动一个 NxN 矩阵,并判断是否存在与某列完全相同的行,并连接相同的行和列的索引。 例如,如果我们有下一个二维数组:
0 1 2
0 [1] [2] [1]
1 [4] [8] [2]
2 [3] [5] [1]
程序会告诉我第 0 行与第 2 列相同。 对不起我的英语不好,我是西班牙人。 所以这是我做的代码:
package Arrays;
public class Ejercicio16 {
public static void main(String[] args) {
int matrix[][];
int i, j;
int rows, columns;
boolean same = false;
//1.Ask inf of the matrix to the user.
do{
rows = Teclado.nextInt("Insert the number of rows of the matrix");
columns = Teclado.nextInt("Insert the number of columns of the matrix");
}while(rows<=0 && columns<=0);
/*Teclado = A java class that I use with methos like nextInt, nextByte, etc with their try catchs and so on.It would be a Scanner method*/
matrix = new int[rows][columns];
for (i=0; i<rows; i++) {
for (j=0; j<columns; j++){
matrix[i][j] = Teclado.nextInt("Insert a value for the cell of the row: "+ i + " column: " + j);
}
}
//4. Show the matrix inserted by the user.
for (i=0; i<rows; i++){
for (j=0; j<columns; j++){
System.out.print( "[" + matrix[i][j] + "]" + " ");
}
System.out.println();
}
checkEquality(matrix, rows, columns, same);
}
public static boolean comprobarIgualdad(int matriz[][], int filas, int columnas, boolean igual){
int i, j;
for (i=0; i<rows; i++){
for (j=0; j<columns; j++){
if(matrix[i] == matrix[j])
same = true;
else
same = false;
}
}
return same;
}
}
现在,我的问题是:我不知道如何制作垂直和水平遍历矩阵并检查它们是否相同的方法。这些天我一直在想,但我找不到答案,所以我想在这里寻求帮助。我想自己解决这个问题,但我想我做不到,所以任何帮助都会受到好评。如果您对我的代码有任何疑问,请告诉我!
【问题讨论】: