我在下面编写了这个示例代码,我希望代码检查条件是否为真,如果为false则等待几分钟再次检查条件是否为真并重复相同的过程直到条件为如果为true,则转到另一行代码。在条件为真之前,我不希望代码在代码下面执行。我知道代码是基本的,但它只是一个例子。
import java.util.concurrent.TimeUnit;
public class Program {
public static void main(String[] args) {
int me = 1;
int you = 19;
int we = 36;
boolean found = false;
while (!found) {
// where to input the TimeUnit for the code to check again if one of the conditions are true so it can move on to the next line of code if there is any?
// TimeUnit.MINUTED.sleep(15);
if(me > you){
System.out.println("I am greater");
found = true;
}
else if(you > we){
System.out.println("(lesser)");
}
else if(we < me){
System.out.println("fine");
}
}
}
}
答案 0 :(得分:0)
我认为计时器是您实现这一目标所需要的。
以下是可能有助于您进行设置的参考:How to set Timer in Java
答案 1 :(得分:0)
首先,您需要某种条件才能更改指定变量的值。即使你放了一个计时器,你也只会进入一个无限循环。对此的一个简单解决方案是只增加一个if语句中的一个变量。
您可以在此处找到适合您主要问题的答案: Stop code until a condition is met