gpt4 book ai didi

java - 重新启动 while 循环

转载 作者:行者123 更新时间:2023-12-02 09:07:33 28 4
gpt4 key购买 nike

我正在尝试重新启动 while 循环。我已经声明了 boolean 类型的变量 keepGoing 。如果 int 变量 x 超出窗口,则 keepGoing 更改为 false。然后reset()方法必须keepGoing=true。它可以工作,但 while 循环不工作。

带有reset()和checkWin()的类:

private void reset() {
b.x = 250;
b.y = 100;
b.keepRunning = true;
a.keepGoing = true;
System.out.println(a.keepGoing);
}

public void checkWin() {
if (b.keepRunning) {
if (b.getX() < -10) {
a.score++;

JOptionPane.showMessageDialog(okno, "Player " + p.getScore()
+ " - Computer " + a.getScore(), "Oh, well...",
JOptionPane.INFORMATION_MESSAGE);
b.keepRunning = false;
a.keepGoing = false;
System.out.println(a.keepGoing);
reset();
} else if (b.getX() > 599) {
p.score++;
JOptionPane.showMessageDialog(okno, "Player " + p.getScore()
+ " - Computer " + a.getScore(), "Good!",
JOptionPane.INFORMATION_MESSAGE);
b.keepRunning = false;
a.keepGoing = false;
System.out.println(a.keepGoing);
reset();
}
}
}

带有线程、keepGoing 和 while 循环的第二个类:

Runnable intel = new Runnable() {
public void run() {
while (keepGoing) {
while (getY() < board.ball.getY()) {
System.out.println(keepGoing + " " + getY());
try {
if (y == 220) {
} else {
y += 1;
Thread.sleep(10);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
while (getY() > board.ball.getY()) {
System.out.println(keepGoing + " " + getY());
try {
if (y == 0) {
} else {
y -= 1;
Thread.sleep(10);
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
};

最佳答案

使用关键字继续转到循环的下一次迭代。例如:

while(true)
{
// ...

if(!condition) continue; // this will go to the beginning of the while loop.

// ...
}

关于java - 重新启动 while 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11437442/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com