qq_36691241 2017-02-18 00:51 采纳率: 29.2%
浏览 831

一个Java的BUG,请大神解答下!!谢谢!!!

public class TetrominoGame {
//测试
public static void main(String[] args) {
//构造T类
TetrominoT t = new TetrominoT(0,4);
printTetrominoGame(t);
//构造J类
TetrominoJ j = new TetrominoJ(0,4);
printTetrominoGame(j);
}
//打印场地
public static void printTetrominoGame(Tetromino tetromino) {
int totalRows = 20;
int totalCols = 10;
Cell[] cells = tetromino.cells;

    for(int row=0;row<totalRows;row++) {
        for(int col=0;col<totalCols;col++) {
            boolean isInCells = false;
            for(int i=0;i<cells.length;i++) {
                if(row == cells[i].row && col == cells[i].col) {
                    System.out.println("*");
                    isInCells = true;
                    break;
                }
            }
        }
        System.out.println();
    }
}

}
class Tetromino {
Cell[] cells;

public Tetromino() {
    cells = new Cell[4];
}
//打印
public void print() {
    String str = "";
    for(int i=0;i<cells.length;i++) {
        str += "(" + cells[i].getCellInfo() + ")";
    }
    System.out.println(str);
}
//下落
public void drop() {
    for(int i=0;i<cells.length;i++) {
        cells[i].row++;
    }
}
//左移
public void leftMove() {
    for(int i=0;i<cells.length;i++) {
        cells[i].col--;
    }
}
//右移
public void rightMove() {
    for(int i=0;i<cells.length;i++) {
        cells[i].col++;
    }
}

}
//T类
class TetrominoT extends Tetromino{

Cell[] cells;
//构造方法
public TetrominoT(int row,int col) {
    super();
    cells = new Cell[4];
    cells[0] = new Cell(row,col);
    cells[1] = new Cell(row,col + 1);
    cells[2] = new Cell(row,col + 2);
    cells[3] = new Cell(row + 1,col);
}

}
//J类
class TetrominoJ extends Tetromino {
Cell[] cells;
//构造方法
public TetrominoJ(int row,int col) {
super();
cells = new Cell[4];
cells[0] = new Cell(row,col);
cells[1] = new Cell(row,col + 1);
cells[2] = new Cell(row,col + 2);
cells[3] = new Cell(row + 1,col);
}

}
//格子
class Cell {
int row;
int col;
public Cell(int row,int col) {
this.row = row;
this.col = col;
}

public String getCellInfo() {
    return row + "," + col;
}

}
这是源代码
图片说明
这是错误信息
请各位帮我调一调

  • 写回答

2条回答 默认 最新

  • ziyejinwei1994 2017-02-18 01:49
    关注

    cells[i].row跟cells[i].col为空,取不出来值

    评论

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器