hooyantsing's Blog

第12次课程

字数统计: 347阅读时长: 1 min
2019/05/10

源辰74班

第12次课程

05.10.2019

内容

1.井字棋盘

模式识别

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import java.util.Random;
import java.util.Scanner;
public class Test1 {
    public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       System.out.println("请录入矩阵行列数:");
       int n = sc.nextInt();
       
       //创建矩阵
       int[][] board = new int[n][n];
       
       //初始化矩阵
       Random r = new Random();
       for(int i=0;i<board.length;i++) {
           for(int j=0;j<board.length;j++) {
               board[i][j] = r.nextInt(2);
               System.out.print(board[i][j]+" ");
           }
           System.out.println();
       }
       
       //检测行
       for(int i=0;i<board.length;i++) {
           boolean isSame = true;
           for(int j=0;j<board.length;j++) {
               if(board[0][0]!=board[i][j])  {
                  isSame = false;
                  break;
               }
           }
           if(isSame) {
               System.out.println("第"+(i+1)+"行全是:"+board[i][0]);
           }
       }
       
       //检测列
       for(int i=0;i<board.length;i++) {
           boolean isSame = true;
           for(int j=0;j<board.length;j++) {
               if(board[0][i]!=board[j][i])  {
                  isSame = false;
                  break;
               }
           }
           if(isSame) {
               System.out.println("第"+(i+1)+"列全是:"+board[0][i]);
           }
       }
       
       //检测主对角
       boolean isSame = true;
       for(int i=1;i<board.length;i++) {
           if(board[0][0]!=board[i][i]) {
               isSame = false;
               break;
           }
       }
       if(isSame) {
           System.out.println("主对角全是:"+board[0][0]);
       }
       
       //检测次对角
       isSame = true;
       for(int i=1;i<board.length;i++) {
           if(board[i][board.length-1]!=board[i][board.length-i-1]) {
               isSame = false;
               break;
           }
       }
       if(isSame) {
           System.out.println("次对角全是:"+board[0][board.length-1]);
       }
    }
}
CATALOG