简体   繁体   中英

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.

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. I'll explain how you could it with just 2 for methods for checkCol .

You would still have your outer 2 for loops

i going from 0 to array.length - 1

j going from 0 to 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).

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. Make sure this flag is set to true before you even enter the for loop.

Outside of the inner for loop, you check if the flag is set to true, if so print that particular column and number. Reset the flag to 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. 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.

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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