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

 

posted @ 2014-03-29 13:55  zhangyongjian  阅读(272)  评论(0编辑  收藏  举报