2

I wrote this example code below, and I want the code to check if the condition is true and if it false to wait for a number of minutes to check again if the condition is true and repeat the same process until the condition is true then move on to the other line of code. I don't want the code to execute below code until the condition is true. I know the code is basic but it just an example.

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");
            }
        }
    }
}
khelwood
  • 55,782
  • 14
  • 81
  • 108
rich25
  • 37
  • 11

2 Answers2

0

I think a Timer is what you need in order to achieve that.

Here's a reference that might help you set it up: How to set Timer in Java

0

First of all, you need some kind of condition in order for the values of the specified variables to change. Even if you put a timer, you will just enter an infinite loop. A simple solution for this is to just increment one of the variables in one of your if-statements.

A suitable answer for your main question can be found here : Stop code until a condition is met

nasdenkov
  • 54
  • 10