2017-10-19 91 views
2

我在下面寫了這個示例代碼,並且我希望代碼檢查條件是否爲真,並且它是否等待幾分鐘來檢查再次如果條件爲真,並重復相同的過程,直到條件爲真,然後移動到另一行代碼。我不希望代碼在條件成立之前執行下面的代碼。我知道代碼是基本的,但它只是一個例子。代碼等待幾分鐘來檢查條件是否爲真

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

使用一個定時器.... – musefan

+0

爲什麼你會認爲這些數字會改變嗎?或者這只是一個例子,實際上你使用某種輸入? – Neo

回答

0

我覺得定時器是你需要爲了實現什麼。

下面是一個參考,可以幫助您設置:How to set Timer in Java

+0

計時器不是你應該使用的東西。 ScheduledThreadPoolExecutor是要走的路 – mlecz

0

首先,你需要某種條件,以便指定變量的改變值。即使你把一個計時器,你只會進入一個無限循環。一個簡單的解決方案是隻增加一個if語句中的一個變量。

你的主要問題一個合適的解決可以在這裏找到: Stop code until a condition is met

相關問題