2013-11-14 34 views
-1

好的我已经被要求做一个反射游戏,这将允许用户启动和停止计时器,然后在最后显示他们的时间。目前它主要是在工作,除了目前有一个问题,它会说推1启动,因为它打算和启动我的计时器,但它会再次显示这个消息之前显示的秒数是不是打算。所以,如果有人可以帮助或者是指出一些东西,可能会导致这将不胜感激打电话时额外输出

public class TimerTest { 

    static int attempts = 4; 
    static Timer timer = new Timer(); 
    static int controler; 
    static int seconds = 1; 
    static Scanner keyboard = new Scanner(System.in); 
    static int time1, time2, time3; 

    public static void run() { 

     Timer timer = new Timer(); 
     timer.scheduleAtFixedRate(new TimerTask() { 

      public void run() { 
       if (controler == 1) { 
        System.out.println(seconds++ + " seconds"); 
       } 
      } // closed the running of timer 

     }, 0, 1000); 
     // closed timer 

    } 

    public static void main(String args[]) { 

     System.out.println("\t\t\tthe rules\n"); 
     System.out.println("1) press 1 then enter to start the game"); 
     System.out.println("2) press 2 then enter as soon as you can to stop the timer"); 
     System.out.println("3)only enter the specified numbers none other"); 
     System.out.println("4) numbers only no letters\n\n"); 

     do { 

      System.out.println("\t\t\tplease push 1 to start"); 
      controler = keyboard.nextInt(); 

      if (controler == 1 && attempts != 0) { 

       run(); 
       attempts--; 

      } else if (controler == 2 && attempts == 3) { 

       System.out.println("stopped "); 
       controler = controler + 1; 
       System.out.println("your time is " + seconds); 
       time1 = seconds; 
       seconds = 0; 

      } else if (controler == 2 && attempts == 2) { 

       System.out.println("stopped "); 
       controler = controler + 1; 
       System.out.println("go 2 " + seconds); 
       time2 = seconds; 
       seconds = 0; 

      } else if (controler == 2 && attempts == 1) { 

       System.out.println("stopped "); 
       controler = controler + 1; 
       time3 = seconds; 
       System.out.println("final go " + seconds); 
       seconds = 0; 
       attempts = 0; 

      } 

      System.out.println("\nyou have " + attempts + " attempts remaining\n"); 

     } while (attempts != 0); 

     if (time1 > time2 && time1 > time3) { 
      System.out.println("your first go was slowest with a time of " + time1 + " seconds"); 
     } else if (time2 > time1 && time2 > time3) { 
      System.out.println("your second go was slowest with a time of " + time2 + " seconds"); 
     } else if (time3 > time1 && time3 > time2) { 
      System.out.println("your final go was slowest with a time of " + time3 + " seconds"); 
     } else { 
      System.out.println("STOP FUCKING UP MY PROGRAM JAVA"); 
     } 

     if (attempts == 0) { 
      System.out.println("\ngame over, thanks for playing and please try again"); 
      System.exit(0); 
     } 

    } 

} 

回答

0

将您do-while循环的

System.out.println("\t\t\tplease push 1 to start"); 

之外。否则,每次迭代时都会打印它,这似乎不止一次。

0

假设你想为有人企图迭代while循环(并显示您的消息)多次:

把你的语句的条件,只打印当控制器不是1

if (controler != 1) { 
    System.out.println("\t\t\tplease push 1 to start"); 
} 
controler = keyboard.nextInt(); 

另请注意,您需要以某种方式停止您的计时器。可能你想保留一个参考,并在按'2'时调用timer.cancel()