我正在制作一个程序来玩pacman并有几个课程,我有一个Wall类,一个Goast类,一个Pacman类,一个Pellot类和一个Board类。所有这些都编译没有错误,除了董事会,它不完整,但我有没有编译, 我拥有的是
import java.awt.*;
import javax.swing.*;
public class Board{
private int xDim_=1000;
private int yDim_=900;
private Pacman pacman_=new Pacman(475,525,0);
private Goast greenredGoast_=new Goast(525,350,2,Color.RED);
private Goast onageGoast_=new Goast(525,275,1, Color.ORANGE);
private Goast pinkGoast_=new Goast(425,350,2,Color.MAGENTA);
private Goast blueGoast_=new Goast(425,275,0,Color.CYAN);
Wall[] walls = new Wall[22];
walls[0]=new Wall(50,850,900,50);
walls[1]=new Wall(0,0,50,900);
walls[2]=new Wall(50,0,900,50);
walls[3]=new Wall(125,125,50,150);
在创建新Wall时,每个条目都会出现100个错误,错误如下:
walls[0]=new Wall(50,850,900,50);
^
Board.java:12: error: ';' expected
walls[0]=new Wall(50,850,900,50);
^
Board.java:12: error: illegal start of type
walls[0]=new Wall(50,850,900,50);
^
Board.java:12: error: <identifier> expected
walls[0]=new Wall(50,850,900,50);
^
Board.java:12: error: ';' expected
walls[0]=new Wall(50,850,900,50);
^
Board.java:12: error: illegal start of type
walls[0]=new Wall(50,850,900,50);
^
Board.java:12: error: <identifier> expected
walls[0]=new Wall(50,850,900,50);
^
Board.java:12: error: <identifier> expected
walls[0]=new Wall(50,850,900,50);
^
Board.java:12: error: illegal start of type
walls[0]=new Wall(50,850,900,50);
^
Board.java:12: error: <identifier> expected
walls[0]=new Wall(50,850,900,50);
^
Board.java:12: error: <identifier> expected
walls[0]=new Wall(50,850,900,50);
^
Board.java:12: error: illegal start of type
walls[0]=new Wall(50,850,900,50);
^
Board.java:12: error: <identifier> expected
walls[0]=new Wall(50,850,900,50);
^
Board.java:12: error: ';' expected
walls[0]=new Wall(50,850,900,50);
^
我正在为每一面墙做到这一点。我不知道有什么问题请帮助!!感谢
答案 0 :(得分:3)
您无法在班级调用walls[0]=new Wall(50,850,900,50);
。
public class Board{
...
Wall[] walls = new Wall[22];
walls[0]=new Wall(50,850,900,50);
walls[1]=new Wall(0,0,50,900);
...
}
您需要将其放在:
中初始化块,
public class Board{
...
Wall[] walls = new Wall[22];
{
walls[0]=new Wall(50,850,900,50);
walls[1]=new Wall(0,0,50,900);
...
}
...
}
构造
public class Board{
...
Wall[] walls = new Wall[22];
public Board(){
walls[0]=new Wall(50,850,900,50);
walls[1]=new Wall(0,0,50,900);
...
}
...
}
或某些方法
public class Board{
...
Wall[] walls = new Wall[22];
public void fillBoard(){
walls[0]=new Wall(50,850,900,50);
walls[1]=new Wall(0,0,50,900);
...
}
...
}
答案 1 :(得分:1)
假设你只需要一个墙阵列,那么我可能会使用一个静态块来初始化我的Wall数组 -
private static Wall[] walls = new Wall[22];
static {
walls[0]=new Wall(50,850,900,50);
walls[1]=new Wall(0,0,50,900);
walls[2]=new Wall(50,0,900,50);
walls[3]=new Wall(125,125,50,150);
...
}
答案 2 :(得分:0)
创建一个方法并将数组代码放在其中 以下代码将起作用
import java.awt.*;
import javax.swing.*;
public class Board{
private int xDim_=1000;
private int yDim_=900;
private Pacman pacman_=new Pacman(475,525,0);
private Goast greenredGoast_=new Goast(525,350,2,Color.RED);
private Goast onageGoast_=new Goast(525,275,1, Color.ORANGE);
private Goast pinkGoast_=new Goast(425,350,2,Color.MAGENTA);
private Goast blueGoast_=new Goast(425,275,0,Color.CYAN);
public void run() { // create a method
Wall[] walls = new Wall[22];
walls[0]=new Wall(50,850,900,50);
walls[1]=new Wall(0,0,50,900);
walls[2]=new Wall(50,0,900,50);
walls[3]=new Wall(125,125,50,150);
}
}