Wei_Smile
李牧羊
采纳率66.7%
2015-04-07 10:10

3x3的二维矩阵里面随机的用0或1填充,找出该矩阵里面的相同行或者列,没有输出

已采纳

package exercise_7;

public class Exercise7_10 {

public static void main(String[] args) {

    int[][] test = new int[3][3];
    for (int i = 0; i < test.length; i++) {
        for (int j = 0; j < test[i].length; j++) {
            int t = (int) (Math.random() * 2);
            test[i][j]=t;
            System.out.print(test[i][j]+ " ");
        }
        System.out.println();
    }//自动生成一个3—3的数组

    boolean flag=true;
for (int i = 0; i < test.length; i++) {
    flag=true;
    for (int j = 0; j < test[i].length-1; j++) {//判断相同行
        if (test[i][j]!=test[i][j+1]) {
            flag=false;
            if (flag) {
                System.out.println("All is on row "+i);
                break;
            }
        }
    }
    for (int j = 0; j < test[i].length-1; j++) {//判断相同列
        if (test[j][i]!=test[j+1][i]) {
            flag=false;
            if (flag) {
                System.out.println("All is on column "+j);
                break;
            }
        }
    }
}


}

}


  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • Wei_Smile 李牧羊 6年前

    package exercise_7;

    public class Exercise7_10 {

    public static void main(String[] args) {
    
        int[][] test = new int[3][3];
        for (int i = 0; i < test.length; i++) {
            for (int j = 0; j < test[i].length; j++) {
                int t = (int) (Math.random() * 2);
                test[i][j] = t;
                System.out.print(test[i][j] + " ");
            }
            System.out.println();
        }// 自动生成一个3—3的数组
    
        boolean flag = true;
        for (int i = 0; i < test.length; i++) {
            flag = true;
            for (int j = 0; j < test[i].length - 1; j++) {// 判断相同行
                if (test[i][j] != test[i][j + 1]) {
                    flag = false;
    
                }
                if (flag) {
                    System.out.println("All is on row " + i);
                    break;
                }
            }
            for (int j = 0; j < test[i].length - 1; j++) {// 判断相同列
                if (test[j][i] != test[j + 1][i]) {
                    flag = false;
    
                }
                if (flag) {
                    System.out.println("All is on column " + j);
                    break;
                }
            }
        }
    
    }
    

    }

    
    
    点赞 评论 复制链接分享