簡體   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