我有一个我在调试时遇到问题的功课问题。该程序的目的是说明哪些行和列具有相同的数字,以及主要和次要对角线。到目前为止,我已经找到了相同的行和列,并打印出来。
这是迄今为止该程序的输出:
0 0 0 0 0 0 0 0
0 0 1 0 1 0 0 0
0 0 0 0 1 0 1 0
0 0 1 0 0 1 1 0
0 0 1 0 0 1 1 0
0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0
0 0 1 1 1 1 1 0
All 0 on row 0
All 0 on column 0
All 0 on column 1
All 0 on column 1
All 0 on column 7
All 0 on column 7
正如您所看到的那样重复了列打印,我无法弄清楚为什么以及如何解决它。我也有一个问题,就是它没有显示第6行是完全一样的。
我想要的输出应该是:
All 0 on row 0
All 0 on row 6
All 0 on column 0
All 0 on column 1
All 0 on column 7
提前谢谢你。
import java.util.Scanner;
public class javaTest
{
// Main method
public static void main(String[] args)
{
int[][] array = {
{0,0,0,0,0,0,0,0},
{0,0,1,0,1,0,0,0},
{0,0,0,0,1,0,1,0},
{0,0,1,0,0,1,1,0},
{0,0,1,0,0,1,1,0},
{0,0,0,0,0,0,1,0},
{0,0,0,0,0,0,0,0},
{0,0,1,1,1,1,1,0}
};
for(int i = 0; i < array.length; i++)
{
for(int j = 0; j < array[i].length; j++)
System.out.print(array[i][j] + " ");
System.out.println();
}
checkRow(array);
checkCol(array);
}
// Check if the row is the same
public static void checkRow(int array[][])
{
String checkRow = "";
int rowCount = 0;
int count = 0;
for(int i = 0; i < array.length; i++)
{
for(int j = 0; j < array[i].length;j++)
{
// Create a new array to compare
int num = array[i][j];
for(int k = 0; k < array[i].length; k++)
{
// Check if the first number of the row is equal to the next
if(num == array[j][k])
// If so increment count
count++;
else
count = 0;
}
// If all numbers of the row is the same, total would be 8 and print
if(count == array.length)
System.out.println("All " + num + " on row " + rowCount);
}
rowCount++;
}
}
// Check if column is the same
public static void checkCol(int array[][])
{
String checkCol = "";
int colCount = 0;
int count = 0;
for(int i = 0; i < array.length; i++)
{
for(int j = 0; j < array[i].length; j++)
{
int num = array[i][j];
for(int k = 0; k < array[i].length; k++)
{
if(num == array[k][i])
count++;
else
count = 0;
}
if(count == array.length)
System.out.println("All " + num + " on column " + colCount);
}
colCount++;
}
}
}
答案 0 :(得分:3)
我认为您的checkRow
或checkCol
方法中不需要3个嵌套for循环。我将解释如何只用2 checkCol
的方法。
你仍然有你的外部2 for循环
i
从0到array.length - 1
j
从0到array[i].length - 1
然后在这个for循环中,检查array[i][j]
处的每个元素是否等于array[0][j]
(该特定列的第0行的元素)。
维护一个布尔标志,如果特定列中的任何元素不等于列第0行的元素,则设置为false。在进入for循环之前,请确保将此标志设置为true。
在内部for循环之外,检查标志是否设置为true,如果是,则打印该特定列和数字。将标志重置为true。
我认为这可能会解决多次打印列的问题。您也可以为行做类似的事情。
如果您不理解任何步骤,请告诉我。
答案 1 :(得分:1)
我相信这是你的问题
if(count == array.length)
System.out.println("All " + num + " on row " + rowCount);
}
rowCount++;// this is inside the first for loop but not the second. so it has to go through all the other inner for loops before it can count this again so its skipping rows
答案 2 :(得分:1)
这个问题可以使用两个for循环来解决,正如@maesydy所建议的那样。这是带输出的实现。
public class Test {
// Main method
public static void main(String[] args) {
int[][] array = {
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 1, 0, 1, 0, 0, 0},
{0, 0, 0, 0, 1, 0, 1, 0},
{0, 0, 1, 0, 0, 1, 1, 0},
{0, 0, 1, 0, 0, 1, 1, 0},
{0, 0, 0, 0, 0, 0, 1, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 1, 1, 1, 1, 1, 0}
};
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++)
System.out.print(array[i][j] + " ");
System.out.println();
}
checkRow(array);
checkCol(array);
}
/**
* Check if all elements of row are same
* @param a array
*/
public static void checkRow(int a[][]) {
for (int r= 0; r < a.length; r++) {
int rowNum = r + 1;
boolean isMatching = true;
for (int c = 0; c < a[r].length -1; c++) {
//Compare two subsequent columns in same column
if(a[r][c] != a[r][c+1]) {
isMatching = false;
break;
}
}
//If all elements matched print output
if(isMatching) {
System.out.println("Row " + rowNum + " has all matching elements");
}
}
}
/**
* Check if all elements of column are same
* @param a array
*/
public static void checkCol(int a[][]) {
for (int c = 0; c < a.length; c++) {
int colNum = c + 1;
boolean isMatching = true;
for (int r = 0; r < a[c].length -1; r++) {
//Compare two subsequent rows in same column
if(a[r][c] != a[r+1][c]) {
isMatching = false;
break;
}
}
//If all elements matched print output
if(isMatching) {
System.out.println("Column " + colNum + " has all matching elements");
}
}
}
}
注意:我使用了名为r / c的索引来描述行索引和列索引。
输出:
0 0 0 0 0 0 0 0
0 0 1 0 1 0 0 0
0 0 0 0 1 0 1 0
0 0 1 0 0 1 1 0
0 0 1 0 0 1 1 0
0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0
0 0 1 1 1 1 1 0
Row 1 has all matching elements
Row 7 has all matching elements
Column 1 has all matching elements
Column 2 has all matching elements
Column 8 has all matching elements
希望这会有所帮助。快乐的节目,享受!