在Java中重置布尔变量
我目前正在编写一个程序,一旦前一个对象满足布尔变量返回true的条件,就会生成一个新对象。我遇到的问题是,我正在处理多个不同的形状。在显示第二种形状之前,该程序工作正常。在这一点上,它有两个符合其布尔isDone()条件的形状。有没有一种简单的方法可以将布尔变量返回为false/其默认条件 代码:在Java中重置布尔变量,java,Java,我目前正在编写一个程序,一旦前一个对象满足布尔变量返回true的条件,就会生成一个新对象。我遇到的问题是,我正在处理多个不同的形状。在显示第二种形状之前,该程序工作正常。在这一点上,它有两个符合其布尔isDone()条件的形状。有没有一种简单的方法可以将布尔变量返回为false/其默认条件 代码: if (baserectangle.isDone()) { int meowchoice = (int) Math.round(2 * Math.random()); if (meow
if (baserectangle.isDone()) {
int meowchoice = (int) Math.round(2 * Math.random());
if (meowchoice < 1) {
baserectangle = (new Rectangle(10, 25, position, m, n));
scene1.add(baserectangle);
}
if (meowchoice >= 1) {
basesquare = (new Square(10, 25, position, m, n));
scene1.add(basesquare);
}}
else if (basesquare.isDone()) {
int meowchoice2 = (int) Math.round(2 * Math.random());
if (meowchoice2 < 1) {
baserectangle = (new Rectangle(10, 25, position, m, n));
scene1.add(baserectangle);
}
if (meowchoice2 >= 1) {
basesquare = (new Square(10, 25, position, m, n));
scene1.add(basesquare);
}
}
if(baserectangle.isDone()){
int meowchoice=(int)Math.round(2*Math.random());
if(meowchoice<1){
baserectangle=(新矩形(10,25,位置,m,n));
场景1.添加(基本矩形);
}
如果(meowchoice>=1){
basesquare=(新的正方形(10,25,位置,m,n));
场景1.添加(basesquare);
}}
else if(basesquare.isDone()){
int meowchoice2=(int)Math.round(2*Math.random());
如果(meowchoice2<1){
baserectangle=(新矩形(10,25,位置,m,n));
场景1.添加(基本矩形);
}
如果(meowchoice2>=1){
basesquare=(新的正方形(10,25,位置,m,n));
场景1.添加(basesquare);
}
}
i、 程序从一个baserectangle或basesquare开始,然后当该形状满足其isDone条件时,随机生成一个新的baserectangle或basesquare。问题是,一旦baserectangle和basesquare都存在,它就会一次连续生成多个形状
isDone()实现:
public boolean isDone() {
if (this.y < 2) {
return true;
}
if (i > 0 && position [i-1][j] != 0) {
return true;
}
else return false;
}
public boolean isDone(){
如果(此.y<2){
返回true;
}
如果(i>0和位置[i-1][j]!=0){
返回true;
}
否则返回false;
}
其中,y是平面上形状的y坐标,i和j是对象在阵列中的位置 您可以在类中放置另一个变量,例如
alreadyDone
,并使用它来决定是否必须生成其他形状,例如:
public boolean isDone() {
if (this.y < 2) {
alreadyDone = true;
return true;
}
if (i > 0 && position [i-1][j] != 0) {
alreadyDone = true;
return true;
}
else return false;
}
请记住,这只是一个例子,我相信有更好的方法可以做到这一点
在显示第二种形状之前,该程序工作正常。在这一点上,它有两个符合其布尔isDone()条件的形状
当然,你有两种形状。问题可能在于这样一个事实,即您只检查one是否使用if-else-if
组合完成
您可能需要检查是否使用单独的if语句完成了或形状
// Rectangle is done
if (baserectangle.isDone()) {
int meowchoice = (int) Math.round(2 * Math.random());
if (meowchoice < 1) {
baserectangle = (new Rectangle(10, 25, position, m, n));
scene1.add(baserectangle);
}
else if (meowchoice >= 1) {
basesquare = (new Square(10, 25, position, m, n));
scene1.add(basesquare);
}
}
// Square is done
if (basesquare.isDone()) {
int meowchoice2 = (int) Math.round(2 * Math.random());
if (meowchoice2 < 1) {
baserectangle = (new Rectangle(10, 25, position, m, n));
scene1.add(baserectangle);
}
else if (meowchoice2 >= 1) {
basesquare = (new Square(10, 25, position, m, n));
scene1.add(basesquare);
}
}
你的字段是静态的吗?唯一静态的是形状的颜色。它的x和y坐标会发生变化,它在位置数组中的位置也会发生变化。那么,是什么控制着
isDone
?你能把它的实现纳入你的问题吗。评论不是为了这个。此外,还应包括该方法所依赖的字段声明。@Makoto已添加到开场白中
// Rectangle is done
if (baserectangle.isDone()) {
int meowchoice = (int) Math.round(2 * Math.random());
if (meowchoice < 1) {
baserectangle = (new Rectangle(10, 25, position, m, n));
scene1.add(baserectangle);
}
else if (meowchoice >= 1) {
basesquare = (new Square(10, 25, position, m, n));
scene1.add(basesquare);
}
}
// Square is done
if (basesquare.isDone()) {
int meowchoice2 = (int) Math.round(2 * Math.random());
if (meowchoice2 < 1) {
baserectangle = (new Rectangle(10, 25, position, m, n));
scene1.add(baserectangle);
}
else if (meowchoice2 >= 1) {
basesquare = (new Square(10, 25, position, m, n));
scene1.add(basesquare);
}
}
// Rectangle or Square are done
if (baserectangle.isDone() || basesquare.isDone()) {
int meowchoice = (int) Math.round(2 * Math.random());
if (meowchoice < 1) {
baserectangle = (new Rectangle(10, 25, position, m, n));
scene1.add(baserectangle);
}
else if (meowchoice >= 1) {
basesquare = (new Square(10, 25, position, m, n));
scene1.add(basesquare);
}
}
public boolean isDone() {
return (this.y < 2) || (i > 0 && position [i-1][j] != 0);
}