第一次写博客,问大家一个问题吧。
本人编辑了一个java五子棋程序,但是运行时出现了问题
import java.util.Scanner; public class FIve { public static void main(String[] args) { new FIve().run(); } public void run() { Scanner scanner = new Scanner(System.in); System.out.println("请输入黑棋昵称"); String nameBlack = scanner.nextLine(); System.out.println("请输入白棋昵称"); String nameWhite = scanner.nextLine(); int size = 15; int[][] table = new int[size][size]; printTable(table, size); int win = 0; int currentPlayer = 0; do { // 下棋 String playerName = currentPlayer == 0 ? nameBlack : nameWhite; System.out.println("该" + playerName + "下棋"); int x, y; boolean canPlace = true; do{ if (!canPlace){ System.out.println("这个位置不能放棋子,请重新输入"); } String code = scanner.nextLine(); y = code.charAt(0) - 'A'; x = Integer.valueOf(code.substring(1)) - 1; canPlace = true; canPlace = canPlace && x >= 0 && x < size; canPlace = canPlace && y >= 0 && y < size; canPlace = canPlace && table[y][x] == 0; }while (!canPlace); table[y][x] = currentPlayer == 0 ? 1 : 2; currentPlayer++; currentPlayer %= 2; // 打印棋盘 printTable(table, size); // 检测输赢 win = check(table, x, y ); } while (win == 0) ; if (win == 1) { System.out.println(nameBlack + "胜利"); } else if (win == 2) { System.out.println(nameWhite + "胜利"); } else if (win == 3) { System.out.println("平局"); } } public void printTable(int[][] table, int size) { System.out.print(" "); for (int i = 0; i < size; i++) { int value = i + 1; System.out.print(value < 10 ? value + " " : value + " "); } System.out.println(); for (int i = 0; i < size; i++) { char title = (char) ('A' + i); System.out.print(title + " "); for (int j = 0; j < size; j++) { int value = table[i][j]; char c = ' '; switch (value) { case 0: c = '.'; break; case 1: c = 'x'; break; case 2: c = 'o'; break; } System.out.print(c + " "); } System.out.println(); } } public int check(int[][] table, int x, int y){ boolean test = false; test = test || moreThaFive(table, x, y, 0, -1); test = test || moreThaFive(table, x, y, 1, -1); test = test || moreThaFive(table, x, y, 1, 0); test = test || moreThaFive(table, x, y, 1, 1); if (test){ return table[y][x]; } return 0; } public boolean moreThaFive(int[][] table, int x, int y, int dx, int dy){ int count = 0; count += count(table, x, y, dx, dy); count += count(table, x, y, -dx, -dy); count -= 1; return count >= 5; } public int count(int[][] table, int originX, int originY, int dx, int dy){ int originValue = table[originY][originX]; int count = 0; int x = originX; int y = originY; int value; do { count ++; originX += dx; originY += dy; value = table[originY][originX]; } while (value == originValue); return count; } }当黑棋输入第一个棋子时,就会出现错误警告,然后程序就会莫名结束。