繁体   English   中英

Java-2D阵列板计数器

[英]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++;
    }
}

}

我认为您不需要在checkRowcheckCol方法中嵌套3个for循环。 我将仅用2个checkCol方法来说明如何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。

我认为这可能会解决多次打印列的问题。 您也可以对行执行类似的操作。

如果您不了解任何步骤,请告诉我。

我相信这是你的问题

    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

正如@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

希望这可以帮助。 快乐编程,尽情享受!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM