简体   繁体   English

Java-2D阵列板计数器

[英]Java - 2D Array Board Counter

I have a homework question that I have trouble debugging. 我有一个作业问题,调试时遇到麻烦。 The purpose of the program is to state which rows and columns have the same numbers, as well as major and minor diagonals. 该程序的目的是说明哪些行和列具有相同的编号,以及主要和次要对角线。 So far I have kind of figured out the rows and columns that are the same, and printing them. 到目前为止,我已经弄清楚了相同的行和列,并进行了打印。

Here is the output of the program thus far: 这是到目前为止的程序输出:

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

As you can see the column print is repeated, and I can't figure out why and how to fix it. 如您所见,列打印是重复的,我不知道为什么以及如何修复它。 I also have a problem in which it is not displaying row 6 as being all the same. 我也有一个问题,即它不显示第6行,因为它们都是一样的。

My desired output should be: 我想要的输出应该是:

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

Thank you in advance. 先感谢您。

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++;
    }
}

} }

I don't think you need 3 nested for loop in your checkRow or checkCol methods. 我认为您不需要在checkRowcheckCol方法中嵌套3个for循环。 I'll explain how you could it with just 2 for methods for checkCol . 我将仅用2个checkCol方法来说明如何checkCol

You would still have your outer 2 for loops 您仍然需要外部2 for循环

i going from 0 to array.length - 1 i从0到array.length - 1

j going from 0 to array[i].length - 1 j从0到array[i].length - 1

Then inside this for loop, you check if every element at array[i][j] is equal to array[0][j] (which is the element at the 0th row for that particular column). 然后在此for循环中,检查array[i][j]处的每个元素是否等于array[0][j] (这是该特定列的第0行的元素)。

Maintain a boolean flag that you set to false if any of the elements in a particular column is not equal to the element at the 0th row of the column. 如果特定列中的任何元素都不等于该列第0行的元素,则保持将其设置为false的布尔标志。 Make sure this flag is set to true before you even enter the for loop. 在进入for循环之前,请确保将此标志设置为true。

Outside of the inner for loop, you check if the flag is set to true, if so print that particular column and number. 在内部for循环之外,您检查标志是否设置为true,如果是,则打印该特定列和数字。 Reset the flag to true. 将标志重置为true。

I think this would probably fix up the issue of printing columns multiple times. 我认为这可能会解决多次打印列的问题。 You could do something similar for the rows as well. 您也可以对行执行类似的操作。

Let me know if you don't understand any of the steps. 如果您不了解任何步骤,请告诉我。

I believe that this is your problem 我相信这是你的问题

    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

This problem can be solved using two for-loops, as suggested by @maesydy. 正如@maesydy所建议的,可以使用两个for循环来解决此问题。 Here is the implementation with output. 这是带有输出的实现。

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");
        }
    }
}

} }

Notes: I have used indexes named r/c to depict row and column index. 注意:我使用了名为r / c的索引来描述行和列的索引。

Output: 输出:

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

Hope this helps. 希望这可以帮助。 Happy programming, enjoy! 快乐编程,尽情享受!

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

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