码迷,mamicode.com
首页 > 其他好文 > 详细

打印五字棋

时间:2016-04-23 23:08:47      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:

public class GoBang {
    // 定义一个二维数组充当棋盘
    private String [][] board;
    //定义棋盘大小
    private static int Board_SIZE =15;
    public void initBorad(){
        board=new String[Board_SIZE][Board_SIZE];
        //把每个元素赋为“+”用于在控制台画出棋盘
        for(int i =0;i<Board_SIZE;i++){
            for(int j=0;j<Board_SIZE;j++){
                board[i][j]="+" ;
            }
        }
    }
    //在控制台输出棋盘的方法
    
    public void printBorad(){
        //把每个元素赋为“+”用于在控制台画出棋盘
        for(int i =0;i<Board_SIZE;i++){
            for(int j=0;j<Board_SIZE;j++){
                //打印数组元素后不换行
            System.out.print(board[i][j]);
            }
            //每打印完一行数组元素后输出一个换行符
            System.out.println();
        }
    }
    public static void main(String[] args) throws IOException {
        GoBang gb = new GoBang();
        gb.initBorad();
        gb.printBorad();
        //这是用于获取键盘输入的方法
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String inputstr=null ;
        //每当在键盘上输入一行内容后按回车键,刚输入的内容将被br读取到
        //String text =br.readLine();
        //System.out.println(text);
        while ((inputstr=br.readLine())!=null){
            //将用户输入的字符串以逗号(,)作为分隔符,分隔成2个字符串
            String [] possStrArr =inputstr.split(",");
            //将2个字符串转换成用户下棋的坐标
            int xPos =Integer.parseInt(possStrArr[0]);
            int yPos =Integer.parseInt(possStrArr[1]);
            //对应的数组元素赋为""
            gb.board[xPos][yPos]="●" ;
            gb.printBorad();
            System.out.println("请输入您下棋的坐标,应以x,y的格式:“");
        }
    }

}

打印五字棋

标签:

原文地址:http://www.cnblogs.com/chizizhixin/p/5425856.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!