package Tetris;
public class TandJProject {
public static void main(String[] args) {
//创建T对象
T t = new T(0,1);
//原始坐标
System.out.println("t的原始坐标是:");
t.print();
//测试T下落
t.drop();
System.out.println("下落后的位置是:");
t.print();
//测试T左移
t.leftMove();
System.out.println("左移后的位置是:");
t.print();
//测试T右移
t.rightMove();
System.out.println("右移后的位置是:");
t.print();
//创建J对象
J j = new J(0,1);
System.out.println("原始坐标是:");
j.print();
//测试J下落
j.drop();
System.out.println("下落后的位置是:");
j.print();
//测试J左移
j.leftMove();
System.out.println("左移后的位置是:");
j.print();
//测试J右移
j.rightMove();
System.out.println("右移后的位置是:");
j.print();
//创建O对象
}
}
//构建T类
class T {
Cell[] cells;
//无参的构造方法
public T() {
this(0,0);
}
//有参的构造方法
public T(int row,int col) {
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 + 1);
}
//打印的方法
public void print() {
String str = "";
for(int i=0;i<=cells.length-1;i++) {
str += "(" + cells[i].getCellInfo() + ")";
}
System.out.println(str);
}
//左移的方法
public void leftMove() {
for(int i=0;i<=cells.length-1;i++) {
cells[i].leftMove();
}
}
//右移的方法
public void rightMove() {
for(int i=0;i<=cells.length-1;i++) {
cells[i].rightMove();
}
}
//下落的方法
public void drop() {
for(int i=0;i<=cells.length-1;i++) {
cells[i].drop();
}
}
}
//构建J类
class J {
Cell[] cells;
//无参的构造方法
public J() {
this(0,0);
}
//有参的构造方法
public J(int row,int col) {
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 + 2);
}
//打印的方法
public void print() {
String str = "";
for(int i=0;i<=cells.length-1;i++) {
str += "(" + cells[i].getCellInfo() + ")";
}
System.out.println(str);
}
//左移的方法
public void leftMove() {
for(int i=0;i<=cells.length-1;i++) {
cells[i].leftMove();
}
}
//右移的方法
public void rightMove() {
for(int i=0;i<=cells.length-1;i++) {
cells[i].rightMove();
}
}
//下降的方法
public void drop() {
for(int i=0;i<=cells.length-1;i++) {
cells[i].drop();
}
}
//创建O类
class O {
Cell[] cells;
//定义构造方法
public O() {
this(0,0);
}
//有参的构造方法
public O(int row,int col) {
cells = new Cell[4];
cells[0] = new Cell(row,col);
cells[1] = new Cell(row,col + 1);
cells[2] = new Cell(row + 1,col);
cells[3] = new Cell(row + 1,col + 1);
}
//打印的方法
public void print() {
String str = "";
for(int i=0;i<=cells.length-1;i++) {
str += "(" + cells[i].getCellInfo() + ")";
}
System.out.println(str);
}
//定义下落
public void drop() {
for(int i=0;i<=cells.length-1;i++) {
cells[i].drop();
}
}
//定义左移
public void leftMove() {
for(int i=0;i<=cells.length-1;i++) {
cells[i].leftMove();
}
}
//定义右移
public void rightMove() {
for(int i=0;i<=cells.length-1;i++) {
cells[i].rightMove();
}
}
}
//构建方格
class Cell {
int row;
int col;
public Cell() {
this(0,0);
}
public Cell(int row,int col) {
this.row = row;
this.col = col;
}
public void leftMove() {
col--;
}
public void rightMove() {
col++;
}
public void drop() {
row++;
}
public String getCellInfo() {
return row + "," + col;
}
}
这是错误信息、
错在哪里?错了那又该怎样改正呢?
你把你完整代码贴出来,我手打的没有错误啊。。
Cells 【】 cells;这个不对吧,一般应该像这样int a = 0;这样,给他一个初始变量啊
没有import cell类???
看起来不是没导入的问题,你贴一下报错信息看看,或者直接贴全部代码(不要截图)。
在这个地方加static,因为main方法是静态的,因此静态方法不能访问【非静态的内部类Cell】,给Cell加上static就变成静态,可以被正常访问了。