2016-03-01 43 views
0

我用“e”变量代替goto! 在我的代码 “E” 变量是未知嵌套块中的未知变量java

int e=1; 

while(e==1) 
{ 
    if (now.getMinute() == end_minute && now.getSecond() >= second) break; 
    new java.util.Timer().schedule(new java.util.TimerTask() { 
     @Override 
     public void run() { 
      System.out.println("hello!"); 
      if (now.getMinute() == end_minute && now.getSecond() >= second) e=0; 
     } 
    }, 60000); 
} 

感谢。

+4

Please.format.your.code。谢谢。 – Thomas

+4

除此之外,它看起来像你在'TimerTask'里的意思是'e = 0'是未知的。那是因为你创建了一个匿名的内部类,只有当它们被声明为final时,才能看到方法局部变量'e'(你不能再改变它们)。 – Thomas

+0

为了在匿名类中使用局部变量'e',你必须将'e'定义为final,而'e = 0'不会再起作用。你可能想把它包装在一个类中,以便能够修改这个值。 – SomeJavaGuy

回答

0

你可以检查这个吗?

package general; 

import java.time.LocalDateTime; 

public class TestTimer { 
public static int e = 1; 

public static void main(String[] args) throws InterruptedException { 
    LocalDateTime now = LocalDateTime.now(); 
    int end_minute = 45; 
    int second = 30; 

    while(e==1) 
    { 

     if (now.getMinute() == end_minute && now.getSecond() >= second) break; 
     new java.util.Timer().schedule(new java.util.TimerTask() { 

      @Override 
      public void run() { 
       System.out.println("hello!"); 
       if (now.getMinute() == end_minute && now.getSecond() >= second) 
        e=0; 
      } 
     }, 60000); 
    } 


} 

}