首页 > 代码库 > 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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。