HW7.14

时间:2023-06-27 22:19:27
HW7.14

HW7.14

 import java.util.Scanner;

 public class Solution
 {
     public static void main(String[] args)
     {
         Scanner input = new Scanner(System.in);
         System.out.print("Enter the size for the matrix: ");
         int size = input.nextInt();

         input.close();

         int[][] array = new int[size][size];
         for(int i = 0; i < size; i++)
         {
             for(int j = 0; j < size; j++)
             {
                 array[i][j] = (int)(Math.random() * 2);
                 System.out.print(array[i][j] + " ");
             }
             System.out.println();
         }

         boolean judgement = true;
         int falseCount = 0;

         for(int i = 0; i < size; i++)
         {
             for(int j = 1; j < size; j++)
                 if(array[i][j] != array[i][j - 1])
                 {
                     judgement = false;
                     falseCount++;
                     break;
                 }
             if(judgement)
                 System.out.println("All " + array[i][0] + "s on row " + i);
             judgement = true;
         }
         if(falseCount == size)
             System.out.println("No same numbers on a row");

         judgement = true;
         falseCount = 0;
         for(int i = 0; i < size; i++)
         {
             for(int j = 1; j < size; j++)
                 if(array[j][i] != array[j - 1][i])
                 {
                     judgement = false;
                     falseCount++;
                     break;
                 }
             if(judgement)
                 System.out.println("All " + array[0][i] + "s on column " + i);
             judgement = true;
         }
         if(falseCount == size)
             System.out.println("No same numbers on a column");

         judgement = true;
         for(int i = 1; i < size; i++)
             if(array[i][i] != array[i - 1][i - 1])
             {
                 judgement = false;
                 break;
             }
         if(judgement)
             System.out.println("All " + array[0][0] + "s on the major diagonal");
         else
             System.out.println("No same numbers on the major diagonal");

         judgement = true;
         for(int i = size - 1; i > 0; i--)
             if(array[i][size - i - 1] != array[i - 1][size - i])
             {
                 judgement = false;
                 break;
             }
         if(judgement)
             System.out.println("All " + array[size - 1][0] + "s on the sub diagonal");
         else
             System.out.println("No same numbers on the sub diagonal");
     }
 }

相关文章