Java学习 day8
今天写了一个Java打印五子棋程序:
package com.dream.Test;
import java.util.Scanner;
public class Test01 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int len = 20;
String black = "━●━";
String white = "━○━";
String[] seStr = {" 1.", " 2.", " 3.", " 4.", " 5.", " 6.", " 7.", " 8.", " 9.", "10.","11.", "12.", "13.", "14.", "15.","16.", "17.", "18.", "19.", "20."};
String[][] goBang = new String[len][len];
for (int i = 0; i < goBang.length; i++) {
for (int j = 0; j < goBang.length; j++) {
goBang[i][j] = "━╋━";
}
}
printChess(goBang, seStr);
boolean bool = false;
while(true){
String isBW = bool ? "黑子" : "白子";
System.out.println("请输入" + isBW + "坐标");
int xChess = scan.nextInt();
int yChess = scan.nextInt();
if(xChess <= len && yChess <= len && goBang[xChess][yChess] == "━╋━"){
goBang[xChess][yChess] = bool ? black : white;
bool = !bool;
}else{
System.out.println("输入错误,请重新输入:");
}
printChess(goBang, seStr);
}
}
public static void printChess(String[][] goBang, String[] seStr){
for (int i = 0; i < goBang.length; i++) {
for (int j = 0; j < goBang.length; j++) {
System.out.print(goBang[i][j]);
}
System.out.println(seStr[i]);
}
for (String string : seStr) {
System.out.print(string);
}
System.out.println();
}
}
|