我正在尝试用Java做一个简单的代码:我有一个名为"Bloc"的类,它创建了块(正方形),在其中我创建了一个随机宽度(largeur),以及一个2 int之间的随机高度(hauteur) ,我创建一个随机数的块(nombreBloc).我还创建了一个ArrayList来放置每个块,看看我剩下多少.
所以,我做了一个名为"insererBlocList"的函数(将块插入到ArrayList中),它创建块的"nombreBloc"(numberBloc)并将其放入ArrayList中.
我有一个图形界面,其中我有一个用于窗口的面板,在其中我有另外两个面板:其中一个是将我创建的每个块放入其中.
这是我的问题,我的函数"insererBlocList"里面有一个"StackOverflowError",这意味着有一个无限循环,但在写完代码路径之后,我看不出我在哪里犯了错误......这里是代码:
集团类:
public class Bloc extends JPanel{
private int hauteur, largeur, nombreBloc;
private boolean premierPassage = true;
private ArrayList<Bloc> listeBlocRestant;
private Random rand = new Random();
public Bloc() {
this.hauteur = 10 + rand.nextInt(50 - 10);
this.largeur = 10 + rand.nextInt(50 - 10);
listeBlocRestant = new ArrayList<Bloc>();
if(premierPassage == true) {
this.nombreBloc = 5 + rand.nextInt(30 - 5);
insererBlocList();
}
}
public ArrayList<Bloc> insererBlocList(){
premierPassage = false;
for(int i=0; i<nombreBloc; i++) {
Bloc bloc = …
Run Code Online (Sandbox Code Playgroud)