题解 | #井字棋#
井字棋
https://www.nowcoder.com/practice/0375c1d206ef48a3894f72aa07b2fdde
import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); char[][] arr = new char[3][3]; for (int i=0; i<3; i++) { for (int j=0; j<3; j++) { arr[i][j] = sc.next().charAt(0); } } // 标记是否有赢家 boolean flag = false; // 先按行检查 for (int i=0; i<3; i++) { if (arr[i][0] == arr[i][1] && arr[i][1] == arr[i][2]){ if (arr[i][0] == 'K') { flag = true; System.out.println("KiKi wins!"); } else if(arr[i][0] == 'B'){ flag = true; System.out.println("BoBo wins!"); } } } // 再按列检查 for (int i=0; i<3; i++) { if (arr[0][i] == arr[1][i] && arr[1][i] == arr[2][i]){ if (arr[0][i] == 'K') { flag = true; System.out.println("KiKi wins!"); } else if(arr[0][i] == 'B'){ flag = true; System.out.println("BoBo wins!"); } } } // 检查主对角线 if (arr[0][0] == arr[1][1] && arr[1][1] == arr[2][2]) { if (arr[0][0] == 'K') { flag = true; System.out.println("KiKi wins!"); } else if(arr[0][0] == 'B'){ flag = true; System.out.println("BoBo wins!"); } } // 检查副对角线 if (arr[0][2] == arr[1][1] && arr[1][1] == arr[2][0]) { if (arr[0][2] == 'K') { flag = true; System.out.println("KiKi wins!"); } else if(arr[0][2] == 'B'){ flag = true; System.out.println("BoBo wins!"); } } if (!flag){ System.out.println("No winner!"); } } }