Java 编写一个程序,测试2d数组中是否有两个1位于同一行或同一列上

Java 编写一个程序,测试2d数组中是否有两个1位于同一行或同一列上,java,algorithm,loops,for-loop,multidimensional-array,Java,Algorithm,Loops,For Loop,Multidimensional Array,我需要编写一个程序,在一个2d数组中循环,该数组的元素由1或0组成,检查一行或一列中是否有两个1,然后打印true,如果在同一列或列中发现两个1,我可以停在那里(我不需要计算1) 所以我计划为行中的1创建一个计数器,为列中的1创建一个计数器,如果该计数器超过1,则循环中断并打印。但是,计数器不会按行或列重置,因此当前如果它找到任何两个1,无论其位置如何,都将打印 我尝试在for循环的末尾为每一个添加rowTotal=0&colTotal=0,但这样做根本找不到任何1 另外,这是我的数据结构和算法

我需要编写一个程序,在一个2d数组中循环,该数组的元素由1或0组成,检查一行或一列中是否有两个1,然后打印true,如果在同一列或列中发现两个1,我可以停在那里(我不需要计算1)

所以我计划为行中的1创建一个计数器,为列中的1创建一个计数器,如果该计数器超过1,则循环中断并打印。但是,计数器不会按行或列重置,因此当前如果它找到任何两个1,无论其位置如何,都将打印

我尝试在for循环的末尾为每一个添加rowTotal=0&colTotal=0,但这样做根本找不到任何1

另外,这是我的数据结构和算法类,所以我需要提供一个完整的算法,所以我不想使用任何函数。任何关于改进我的代码或解决这个问题的更好方法的建议都将不胜感激。我可以用Python或Java来实现这一点

非常感谢

int[][] board = new int[4][4];



// number to look for
        int findNum = 1;
        // initial total 
        int total = 0;  
        // flag variable to end loop
        boolean found = false;       
        // loops only if found is not 
        for (int i = 0; i < board.length && !found; i++)
        {     
            // resets for each new iteration 
            total = 0;  
            // loops only if found is not 
            for(int j = 0; j < board[i].length && !found; j++)
            {              
                //check row
                if(board[i][j] == findNum) {
                    total++;
                }
                // check column
                if(board[j][i] == findNum) {
                    total++;
                }
                // if more total greater than 1 then end
                if(total > 1) {
                    found = true;
                }
            }

        }

int[]board=newint[4][4];
//要查找的号码
int findNum=1;
//初始总数
int-total=0;
//结束循环的标志变量
布尔值=false;
//仅当未找到时循环
对于(int i=0;i1){
发现=真;
}
}
}

如果使用
colTotal
而不是
rowTotal

 if (colTotal > 1) {
   System.out.println("2 lying on col");
   break;
 } 
记住这个
break
不会中断外部循环,所以您需要为eg设置一个标志

 boolean found = false; // outside loops
打印和中断时,只需指定
true

 found = true; // before break inside inner loop, adjecent to print statement
现在使用此标志检查外部循环中的第一条语句

 if (found) {
    break;
 }
或者,您可以将其作为外部循环中的条件

 for (int i = 0; i < board.length && !found; i++)
for(int i=0;i
我认为您可以逐行遍历整个2d数组,检查是否有多个1,现在将数组转置,并重复此过程以逐列检查:

 public static void main(String[] args) {

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

    if (checkMoreThanOne(board)) {
        System.out.println("Found more than one 1's in rows.");
    }
    int[][] transpose = transpose(board);
    if (checkMoreThanOne(transpose)) {
        System.out.println("Found more than one 1's in columns.");
    }
}

private static boolean checkMoreThanOne(int[][] board) {
    for (int i = 0; i < board.length; i++) {
        int count = 0;
        for (int j = 0; j < board[i].length; j++) {
            if (board[i][j] == 1) {
                if (count > 0)
                    return true;
                else
                    count++;
            }
        }
    }
    return false;
}

private static int[][] transpose(int[][] array) {
    if (array == null || array.length == 0)
        return array;

    int width = array.length;
    int height = array[0].length;

    int[][] array_new = new int[height][width];

    for (int x = 0; x < width; x++) {
        for (int y = 0; y < height; y++) {
            array_new[y][x] = array[x][y];
        }
    }
    return array_new;
}
publicstaticvoidmain(字符串[]args){
int[][]板={{0,0,0,0},
{ 0, 1, 0, 0 }, 
{ 1, 0, 0, 1 }, 
{ 0, 0, 1, 0 } };
如果(检查多个(板)){
System.out.println(“在行中找到多个1”);
}
int[][]转置=转置(板);
如果(选中多个(转置)){
System.out.println(“在列中找到多个1”);
}
}
专用静态布尔校验多个(int[][]板){
对于(int i=0;i0)
返回true;
其他的
计数++;
}
}
}
返回false;
}
专用静态int[][]转置(int[][]数组){
if(array==null | | array.length==0)
返回数组;
int width=array.length;
int height=数组[0]。长度;
int[]数组_new=新int[高度][宽度];
对于(int x=0;x
创建两个集合:一个用于行,另一个用于列

在数组中循环


每次找到具有设置位的单元格时,请检查集合中该单元格的行和列。如果这两种情况都存在,你就完了。如果没有,请更新集合并继续。

您可以替换系统。退出(0);与适当的回报声明等惊人的,谢谢你这么多。然而,这将只检查下一个元素是否为1,而我只需要检查它们是否在同一行上。因此,如果第一行是1001,那么我应该打印true。是否仍然不能更改行:如果(j