Java - 2D阵列检查对角线编号板 [英] Java - 2D array checking diagonal number board

查看:154
本文介绍了Java - 2D阵列检查对角线编号板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在开发一个在8x8 2D阵列板中生成随机0和1的程序。我要做的是检查对角线上的所有数字是否相同(从角落开始,而不仅仅是任何对角线)

Currently I'm working on a program that generates random 0's and 1's in a 8x8 2D array board. What I have to do is check whether or not if all the numbers on the diagonal are the same (starting from the corners, not just any diagonals)

示例:

int[][] array = {
    {0, 0, 0, 0, 0, 0, 0, 1},
    {0, 0, 1, 0, 1, 0, 1, 0},
    {0, 0, 0, 0, 1, 1, 1, 0},
    {0, 0, 0, 0, 1, 1, 1, 0},
    {0, 0, 1, 1, 0, 1, 1, 0},
    {0, 0, 1, 0, 0, 0, 1, 0},
    {0, 1, 0, 0, 0, 0, 0, 0},
    {1, 0, 0, 1, 1, 1, 1, 0}
    };

因此,如果偶然所有数字从左上角开始(0,0),(1 ,1)...(7,7)都是0或1,然后我必须输出到扫描仪,指示有一个0的主要对角线(来自上面的例子)。

So if by chance all the numbers starting from the top left corner (0,0),(1,1)...(7,7) are all either 0's or 1's then I have to output to the scanner indicating that "There is a major diagonal of 0" (from the example above).

同样从这个例子中,我们可以看到,从右上角开始,数字1在左下角对角重复,然后我还要显示有一个小的对角线为1。

Also from this example, we can see that from the top-right, the number "1" is repeated diagonally towards the bottom left, then I also have to display "There is a minor diagonal of 1".

到目前为止,我已经想出了如何生成数字并输入数组,但我不知道如何检查。这是我到目前为止:

So far I have figured out how to generate and input the numbers into the array, but I don't know how to check. This is what I have so far:

public class javaTest{
// Main method
public static void main(String[] args) {

    int[][] array = {
    {0, 0, 0, 0, 0, 0, 0, 1},
    {0, 0, 1, 0, 1, 0, 1, 0},
    {0, 0, 0, 0, 1, 1, 1, 0},
    {0, 0, 0, 0, 1, 1, 1, 0},
    {0, 0, 1, 1, 0, 1, 1, 0},
    {0, 0, 1, 0, 0, 0, 1, 0},
    {0, 1, 0, 0, 0, 0, 0, 0},
    {1, 0, 0, 1, 1, 1, 1, 0}
    };

    // Print array numbers
    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();
    }
    // Print checkers

    checkMajorDiagonal(array);
}
// Check major diagonal 
public static void checkMajorDiagonal(int array[][]) {
    int majDiag;
    boolean isMatching = true;
    int row = 0;
    for(row = 0; row < array.length; row++){
        majDiag = row;
        if(array[row][row] != array[row+1][row+1]){
            isMatching = false;
            break;
        }
    }
    //If all elements matched print output
    if(isMatching)  
        System.out.println("Major diagnol is all " + array[row][row]);
}
}

虽然到目前为止我的工作并不像我一样希望它是因为有错误,我仍然需要做小角度。提前致谢。

Though what I have so far is not working as I want it to be as there is an error, and I still have to do the minor diagonal. Thanks in advance.

推荐答案

已经有很多答案了。这是另一种方法。你是在正确的轨道上,但是没有必要通过检查具有下一个元素的对角线元素来使事情变得复杂,依此类推。只需用第一个对角线元素检查每个对角元素。当你发现差异时,你就停止检查了!

There are a bunch of answers already. Here is one more way to do it. You are on the right track, but there is no need to complicate things by checking a diagonal element with the next element and so on. Just check each diagonal element with the first diagonal element. The moment you find a difference, you stop checking!

 public static void checkDiagonal(int[][] array){

     // Start with the assumption that both diagonals are consistent.
     boolean majorConsistent = true; 
     boolean minorConsistent = true;

     int length = array.length;

     int tempMajor = array[0][0];        // all elements in the Major must be equal to this
     int tempMinor = array[0][length-1]; // all elements in the Minor must be equal to this

     // Check major diagonal, and update the boolean if our assumption is wrong.
     for(int i=0; i<length; i++){ 
         if (array[i][i] != tempMajor) { //(0,0);(1,1);(3,3);...
             majorConsistent = false;
             break;
         }
     }

     // Check minor diagonal, and update the boolean if our assumption is wrong.
     for(int i=0,j=length-1; i<length; i++,j--){
         if (array[i][j] != tempMinor) { //(0,7);(1,6);(2,5);...
             minorConsistent = false;
             break;
         }
     }

     System.out.println("Major elements all same = "+majorConsistent);
     System.out.println("Minor elements all same = "+minorConsistent);

 }

这样你仍然可以在 O(n),您不需要嵌套for循环! 注意您可以优化此代码以删除冗余,即具有单个for循环等。

This way you are still doing both the checks in O(n) and you don't need nested for loops! Note that you can refine this code to remove redundancy i.e. have a single for loop, etc.

这篇关于Java - 2D阵列检查对角线编号板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆