目前没想到好方法,等以后想到了再更新吧
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
char[][] array = new char[3][3];
for(int i = 0 ; i < 3 ; i++) {
for(int j = 0 ; j < 3 ; j++) {
array[i][j] = sc.next().charAt(0);
}
}
// 行列斜线分别组成str
StringBuilder str1 = new StringBuilder();
str1.append(array[0][0]);
str1.append(array[0][1]);
str1.append(array[0][2]);
StringBuilder str2 = new StringBuilder();
str2.append(array[1][0]);
str2.append(array[1][1]);
str2.append(array[1][2]);
StringBuilder str3 = new StringBuilder();
str3.append(array[2][0]);
str3.append(array[2][1]);
str3.append(array[2][2]);
StringBuilder str4 = new StringBuilder();
str4.append(array[0][0]);
str4.append(array[1][0]);
str4.append(array[2][0]);
StringBuilder str5 = new StringBuilder();
str5.append(array[0][1]);
str5.append(array[1][1]);
str5.append(array[2][1]);
StringBuilder str6 = new StringBuilder();
str6.append(array[0][2]);
str6.append(array[1][2]);
str6.append(array[2][2]);
StringBuilder str7 = new StringBuilder();
str7.append(array[0][0]);
str7.append(array[1][1]);
str7.append(array[2][2]);
StringBuilder str8 = new StringBuilder();
str8.append(array[0][2]);
str8.append(array[1][1]);
str8.append(array[2][0]);
boolean flag = false; // 没有结果
if(str1.toString().equals("KKK") || str2.toString().equals("KKK") || str3.toString().equals("KKK") || str4.toString().equals("KKK") || str5.toString().equals("KKK") || str6.toString().equals("KKK") || str7.toString().equals("KKK") || str8.toString().equals("KKK")) {
System.out.println("KiKi wins!");
flag = true;
} else if(str1.toString().equals("BBB") || str2.toString().equals("BBB") || str3.toString().equals("BBB") || str4.toString().equals("BBB") || str5.toString().equals("BBB") || str6.toString().equals("BBB") || str7.toString().equals("BBB") || str8.toString().equals("BBB")) {
System.out.println("BoBo wins!");
flag = true;
}
if(!flag) {
System.out.println("No winner!");
}
}
}