首页 > 代码库 > HW7.14

HW7.14

技术分享

 

 1 import java.util.Scanner; 2  3 public class Solution 4 { 5     public static void main(String[] args) 6     { 7         Scanner input = new Scanner(System.in); 8         System.out.print("Enter the size for the matrix: "); 9         int size = input.nextInt();10 11         input.close();12 13         int[][] array = new int[size][size];14         for(int i = 0; i < size; i++)15         {16             for(int j = 0; j < size; j++)17             {18                 array[i][j] = (int)(Math.random() * 2);19                 System.out.print(array[i][j] + " ");20             }21             System.out.println();22         }23 24 25         boolean judgement = true;26         int falseCount = 0;27 28         for(int i = 0; i < size; i++)29         {30             for(int j = 1; j < size; j++)31                 if(array[i][j] != array[i][j - 1])32                 {33                     judgement = false;34                     falseCount++;35                     break;36                 }37             if(judgement)38                 System.out.println("All " + array[i][0] + "s on row " + i);39             judgement = true;40         }41         if(falseCount == size)42             System.out.println("No same numbers on a row");43 44         judgement = true;45         falseCount = 0;46         for(int i = 0; i < size; i++)47         {48             for(int j = 1; j < size; j++)49                 if(array[j][i] != array[j - 1][i])50                 {51                     judgement = false;52                     falseCount++;53                     break;54                 }55             if(judgement)56                 System.out.println("All " + array[0][i] + "s on column " + i);57             judgement = true;58         }59         if(falseCount == size)60             System.out.println("No same numbers on a column");61 62         judgement = true;63         for(int i = 1; i < size; i++)64             if(array[i][i] != array[i - 1][i - 1])65             {66                 judgement = false;67                 break;68             }69         if(judgement)70             System.out.println("All " + array[0][0] + "s on the major diagonal");71         else72             System.out.println("No same numbers on the major diagonal");73 74         judgement = true;75         for(int i = size - 1; i > 0; i--)76             if(array[i][size - i - 1] != array[i - 1][size - i])77             {78                 judgement = false;79                 break;80             }81         if(judgement)82             System.out.println("All " + array[size - 1][0] + "s on the sub diagonal");83         else84             System.out.println("No same numbers on the sub diagonal");85     }86 }

 

HW7.14