2013-10-21 3 views
1

저는 Java 초보자입니다. 3-4 시간 동안이 타이머 문제를 해결하려고 노력했습니다. 인터넷에서 거의 모든 것을 시도했습니다.Java Time Pause

중요한 것은 새로운 게임을 시작하거나 10 초를 기다리면 아무 것도 입력하지 않아도 메뉴로 리디렉션됩니다. 내 코드처럼 보이는 방법

이입니다

long startTime = System.currentTimeMillis(); 
long maxDurationInMilliseconds = 10000; 

while (System.currentTimeMillis() < startTime + maxDurationInMilliseconds) { 
Scanner end = new Scanner (System.in); 
System.out.println("Enter anything if you want to start a new game or wait 10 seconds and you will be redirected to the Menu"); 
    String value; 
    value = end.nextLine(); 

    if (value != null) { 
     playGame(); 
    } 

    else if (System.currentTimeMillis() > startTime + maxDurationInMilliseconds) { 
    // stop running early 
     showMainMenu(); 
    break; 
} 

}

그러나

내가이 일을 얻을 수없는 어떤 이유로,이 작업을 얻기 위해 고투하고 유래 내 마지막 기회.

편집 : 답장을 보내 주신 모든 분들께 감사드립니다. 아직 고쳐지지 않았고, 두통을 앓고 있으며 오전 3시 31 분입니다.

+2

는'end.nextLine'은 차단 방법이다. 당신이 무언가를 입력 할 때까지는 무언가를 기다릴 것입니다. – MadProgrammer

+0

브래킷이 없습니다. 배치는 어디입니까? –

+1

멀티 스레딩 없이는이 작업을 수행 할 수 없습니다. – bstempi

답변

1
Using

TimerTask (http://docs.oracle.com/javase/7/docs/api/java/util/TimerTask.html)

public class MyClass { 
    private static final int TEN_SECONDS = 10000; 
    private String userInput = ""; 

    TimerTask timerTask = new TimerTask(){ 
     public void run() { 
      if(userInput.equals("")) 
       showMainMenu(); 
     } 
    }; 

    public void getInput() throws Exception { 
     Timer timer = new Timer(); 
     timer.schedule(timerTask, TEN_SECONDS); 

     System.out.println("Press any key or wait 10 seconds to be redirected to the Menu."); 
     BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 
     userInput = in.readLine(); 

     timer.cancel(); 
     if (!userInput.equals("")) 
      playGame(); 
    } 

    public static void main(String[] args) { 
     try { 
      (new MyClass()).getInput(); 
     } catch(Exception e){ 
      System.out.println(e.getMessage()); 
     } 
    } 
} 
+0

그건 작동하지 않습니다. 'hasNext()'는 입력 버퍼에 무언가가있을 때까지 리턴하지 않습니다. – tbodt

+0

감사합니다. 코드를 수정했습니다. – iamreptar

0
//make this booean part of the class and not function 
Boolean isStopped = false;  

System.out.println("Enter anything to start new game."); 
Scanner end = new Scanner (System.in); 

final Thread startThread = new Thread(new Runnable(){ 
    public void run(){ 
     try{ 
      Thread.sleep(10000); 
      if(!isStopped) 
       showMenu(); 
     }catch(Exception e){ 
      e.printStackTrace(); 
     } 
    } 
}); 
final Thread inputThread = new Thread(new Runnable(){ 
    public void run(){ 
     end.nextLine(); 
     isStopped = true; 
     startGame(); 
    } 
}); 
inputThread.start(); 
startThread.start(); 
+0

'stop'은 더 이상 사용되지 않으며 * "본질적으로 안전하지 않습니다"* - [Java Thread Primitive Deprecation] (http://docs.oracle.com/javase/7/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.html) for for를 참조하십시오. 자세한 내용은 ... – MadProgrammer

+0

좋아, 해결되었습니다. 제게 알려 줘서 고마워요! – john01dav

+0

이것은'end.nextLine' 메소드에서 막힌'inputThread'을 무기한으로 남겨 둘 것입니다. 그것도 미래의 입력 기능에 영향을 미칠 수 있습니다 ... – MadProgrammer