Exploring Matrix

import java.util.Scanner;
public class J714
{
    /**
     * @taking input from user
     */
    public static void main(String[] args)
    {
        /**
         * @taking input from user
         */
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the length of a square matrix: ");
        int n = input.nextInt();
        /**
         * @declaring the array of n*n size
         */
        int[][] board = new int[n][n];

        boolean isSameOnARow = false; boolean isSameOnAColumn = false;
        boolean isSameOnADiagonal = false; boolean isSameOnASubdiagonal = false;
        /**
         * @performing the fucntions
         */
        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[0].length; j++) {
                board[i][j] = (int)(Math.random() * 2.0D);
                System.out.print(board[i][j]);
            }
            System.out.println();
        }
        /**
         * @taking a for loop
         */
        for (int i = 0; i < board.length; i++) {
            boolean same = true;
            for (int j = 1; j < board[0].length; j++) {
                /**
                 * @checking the conditions
                 */
                if (board[i][0] != board[i][j]) {
                    same = false;
                    break;
                }
            }
            /**
             * @checking the conditions
             */
            if (same) {
                System.out.println("All " + board[i][0] + "'s on row " + i);
                isSameOnARow = true;
            }
        }
        for (int j = 0; j < board[0].length; j++) {
            boolean same = true;
            for (int i = 1; i < board.length; i++) {
                if (board[0][j] != board[i][j]) {
                    same = false;
                    break;
                }
            }
            if (same) {
                System.out.println("All " + board[0][j] + "'s on column " + j);
                isSameOnAColumn = true;
            }
        }
        boolean same = true;
        for (int i = 1; i < board.length; i++) {
            if (board[0][0] != board[i][i]) {
                same = false;
                break;
            }
        }
        if (same) {
            System.out.println("All " + board[0][0] + "'s on major diagonal");
            isSameOnADiagonal = true;
        }/**
         * @checking the conditions
         */
        same = true;
        for (int i = 1; i < board.length; i++) {
            if (board[0][(board.length - 1)] != board[i][(board.length - 1 - i)]) {
                same = false;
                break;
            }
        }
        /**
         * @checking the conditions and printing the required elements
         */
        if (same) {
            System.out.println("All " + board[0][(board.length - 1)] + "'s on sub-diagonal");
            isSameOnASubdiagonal = true;
        }
        if (!isSameOnARow) {
            System.out.println("No same numbers on a row");
        }
        if (!isSameOnAColumn) {
            System.out.println("No same numbers on a column");
        }
        if (!isSameOnADiagonal) {
            System.out.println("No same numbers on the major diagonal");
        }
        if (!isSameOnASubdiagonal)
            System.out.println("No same numbers on the sub-diagonal");
    }
}
import java.util.Scanner;
public class ExploringMatrix
{
  public static void main (String[] args)
  {
    Scanner scan = new Scanner(System.in);
    System.out.print("Enter the size for the matrix: ");
    int size = scan.nextInt();
    int[][] m = new int[size][size];
     
    for (int i =0;i<size;i++)
    {
      for (int j =0;j<size;j++)
      {
        m[i][j] = (int) (Math.random()*2);
        System.out.print(m[i][j]);
      }
      System.out.println();
    }
    boolean letitbe;
    //Row
    int j =0;
     
    boolean row=false, column=false, major =false, sub = false;
    for(int i=0; i<size;i++)
    {
      letitbe = true;
      for (j =0;j<size-1;j++)
      {
        if (m[i][j] != m[i][j+1]) letitbe = false;
      }
      if (letitbe)
      {
        System.out.println("All " + m[i][j] + "s on row " + i);
        row = true;
      }
    }
    //Column
    for(int i=0; i<size;i++)
    {
      letitbe = true;
      for (j =0;j<size-1;j++)
      {
        if (m[j][i] != m[j+1][i]) letitbe = false;
      }
      if (letitbe)
      {
        System.out.println("All " + m[j][i] + "s on column " + i);
        column = true;
      }
       
    }
    //Major diagonal, there is only one eh?
    letitbe = true;
    for (int  i=0;i<size-1;i++)
    {
      if (m[i][i]!= m[i+1][i+1]) letitbe = false;
    }
    if (letitbe)
    {
      System.out.println("All " + m[0][0] + "s on major diagonal");
      major = true;
    }
     
    letitbe = true;
    for (int  i=0;i<size-1;i++)
    {
      if (m[i][size-i-1]!= m[i+1][size-i-1-1]) letitbe = false;
    }
    if (letitbe)
    {
      System.out.println("All " + m[0][size-1] + "s on sub-diagonal");
      sub = true;
    }
   
    if ( column == false) System.out.println("No same numbers on a column");
    if ( row == false) System.out.println("No same numbers on a row");
    if ( major == false) System.out.println("No same numbers on the major diagonal");
    if ( sub == false) System.out.println("No same numbers on the sub-diagonal");
  }
}
Exploring Matricies in Java
Problem:
Write a program that prompts the user to enter the length of a square matrix, randomly fills in 0s and 1s into the matrix,
prints the matrix, and finds the rows, columns, and diagonals with all 0s or 1s. Output: Enter the size
for the matrix: 4 1000 0111 0101 0100 No same numbers on a column No same numbers on a row No same numbers on the major diagonal No same numbers on the sub-diagonal

 

转载于:https://www.cnblogs.com/zhangyongjian/p/3632463.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值