如果发现二维数组的主要和次要的对角线由0 [英] Finding if the major and minor diagonals of 2D array are comprised of 0's

查看:167
本文介绍了如果发现二维数组的主要和次要的对角线由0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是学校的问题,我工作的一个前奏Java类。的分配是编写生成随机生成二进制数的一个8×8矩阵的程序,并具有程序检查,如果有的话,列均为0的,如果主要和次要对角线也包含零的。一个主要的对角线距离左上角到右下角形成的对角线(即arrayName中[0] [0]至arrayName中[8] [8]在这种情况下)和次对角线是一个从顶部右去角球开到左下方矩阵的角落。

This is a school problem I am working on for an intro Java class. The assignment is to write a program that generates an 8 x 8 matrix of randomly generated binary numbers and have the program check which, if any, columns are all 0's and if the major and minor diagonals are also comprised of zeroes. A major diagonal is the diagonal formed from the top left corner to the bottom right corner (ie arrayName[0][0] to arrayName[8][8] in this case) and a minor diagonal is one that goes from the top right corner to the bottom left corner of the matrix.

我已经得到了一切,除了该检查主要和次要对角线的一部分工作,我想不通这是为什么不工作。我一直在努力做的就是数零的个数沿对角线,如果该号码是8,那么你已经有了自己的对角线由0的。这里有两种方法,我对主要和次要的数组:

I've gotten everything working except for the part that checks for the major and minor diagonals, and I can't figure out why this isn't working. What I've been trying to do is just count the number of zeros along the diagonal, and if that number is 8, then you've got yourself a diagonal comprised of 0's. Here are the two methods that I have for the major and minor array:

public static void majorDiagonal(int[][] board)
    {
        byte count = 0;

        for(int row = board.length - 1, offsetNumber = board.length - 1; row > 0; row--, offsetNumber--)
    	   for(int column = board.length - 1; column > 0; column--)
    		   if(board[row][offsetNumber] == 0) count++;

        if(count == 8) System.out.println("All zeroes on the major diagonal");
    }

public static void minorDiagonal(int[][] board)
    {
        byte count = 0;

        for(int row = board.length - 1, offsetNumber = 0; row > 0; row--, offsetNumber++)
    	   for(int column = board.length - 1; column > 0; column--)
    		   if(board[row][offsetNumber] == 0) count++;

        if(count == 8) System.out.println("All zeroes on the minor diagonal");
    }

这是我遇到的,当我试图在for循环,即计数找到主对角线一个有趣的错误:

An interesting error that I encountered when I tried to find the major diagonal by counting up in the for loop, ie:

for(int row = 0; row< board.length; row++)
    	for(int column = 0; column < board.length; column++)
    		if(board[row][row] == 0) count++;

在code是行不通的,但如果对角线有全1和一个0,它会打印出全零在主对角线,即使变量,算,是不是八强。

The code wouldn't work, but if the diagonal had all 1's and a single 0, it would print "All zeros on the major diagonal" even though the variable, count, wasn't eight.

希望这是有道理的,感谢您的帮助。

Hope this makes sense, thanks for any help.

推荐答案

您是过于复杂的事情。考虑一下,你需要检查元素的位置。为一个主要对角线,该指数是:(0,0),(1,1),(2,2),...,(7,7)。现在暂停,观察变化的格局,并认为:有多少循环,你实际上需要产生这样一个序列?次对角线只有表面不同:(0,7),(1,6),(2,5),...,(7,0) - 这可以改写为:(7-7,7),( 7-6,6),(7-5,5),...(7-0,0)。再次,考虑你实际上有多少需要循环

You're overcomplicating things. Consider for a moment the positions of elements that you need to check. For a major diagonal, the indices are: (0,0), (1,1), (2,2), ..., (7,7). Now pause, observe the pattern of changes, and think: how many loops do you actually need to generate such a sequence? Minor diagonal is only superficially different: (0,7), (1,6), (2,5), ..., (7,0) - which can be rewritten as: (7-7, 7), (7-6, 6), (7-5, 5), ... (7-0, 0). Again, consider how many loops you actually need.

这篇关于如果发现二维数组的主要和次要的对角线由0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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