Java - 二维阵列板计数器
Java - 2D Array Board Counter
我有一个家庭作业问题,我无法调试。该程序的目的是说明哪些行和列具有相同的数字,以及主要和次要对角线。到目前为止,我已经弄清楚了相同的行和列,并将它们打印出来。
到目前为止,这是程序的输出:
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++;
}
}
}
我相信这是你的问题
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
我认为您的 checkRow
或 checkCol
方法中不需要 3 个嵌套的 for 循环。我将解释如何仅使用 checkCol
的 2 个方法。
你仍然会有外层的 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,如果是,则打印特定的列和数字。将标志重置为真。
我认为这可能会解决多次打印列的问题。您也可以对行执行类似的操作。
如果您不理解任何步骤,请告诉我。
按照@maesydy 的建议,这个问题可以使用两个for 循环来解决。这是带输出的实现。
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
希望这对您有所帮助。快乐的编程,享受!
我有一个家庭作业问题,我无法调试。该程序的目的是说明哪些行和列具有相同的数字,以及主要和次要对角线。到目前为止,我已经弄清楚了相同的行和列,并将它们打印出来。
到目前为止,这是程序的输出:
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++;
}
}
}
我相信这是你的问题
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
我认为您的 checkRow
或 checkCol
方法中不需要 3 个嵌套的 for 循环。我将解释如何仅使用 checkCol
的 2 个方法。
你仍然会有外层的 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,如果是,则打印特定的列和数字。将标志重置为真。
我认为这可能会解决多次打印列的问题。您也可以对行执行类似的操作。
如果您不理解任何步骤,请告诉我。
按照@maesydy 的建议,这个问题可以使用两个for 循环来解决。这是带输出的实现。
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
希望这对您有所帮助。快乐的编程,享受!