Java - Countdown timer without GUI

删除回忆录丶 提交于 2019-12-10 13:22:55

问题


Basically I am making a text based "game" (Not so much a game, more of a way to improve basic java skills and logic). However, as part of it I wish to have a timer. It would count down on the time I wish from the variable to 0. Now, I have seen a few ways to do this with a gui, however, is there a way to do this without a gui/jframe etc.

So, what I am wondering is. Can you make a count down from x to 0 without using a gui/jframe. If so, how would you go about this?

Thanks, once I have some ideas will edit with progress.

Edit

// Start timer
Runnable r = new TimerEg(gameLength);
new Thread(r).start();

Above is how I am calling the thread/timer

public static void main(int count) {

If I then have this in the TimerEg class, the timer complies. However, when compiling the main in the other thread I get.

Now, am I completely miss-understanding threads and how this would work? Or is there something I am missing?

Error:

constructor TimerEg in class TimerEg cannot be applied to given types;
required: no arguments; found int; reason: actual and formal arguments differ in length

Found on line Runnable r = new TimerEg(gameLength);


回答1:


Same as with a GUI, you'd use a Timer, but here instead of using a Swing Timer, you'd use a java.util.Timer. Have a look at the Timer API for the details. Also have a look at the TimerTask API since you would use this in conjunction with your Timer.

For example:

import java.util.Timer;
import java.util.TimerTask;

public class TimerEg {
   private static TimerTask myTask = null;
   public static void main(String[] args) {
      Timer timer = new Timer("My Timer", false);
      int count = 10;
      myTask = new MyTimerTask(count, new Runnable() {
         public void run() {
            System.exit(0);
         }
      });

      long delay = 1000L;
      timer.scheduleAtFixedRate(myTask, delay, delay);
   }
}

class MyTimerTask extends TimerTask {
   private int count;
   private Runnable doWhenDone;

   public MyTimerTask(int count, Runnable doWhenDone) {
      this.count = count;
      this.doWhenDone = doWhenDone;
   }

   @Override
   public void run() {
      count--;
      System.out.println("Count is: " + count);
      if (count == 0) {
         cancel();
         doWhenDone.run();
      }
   }

}



回答2:


You could write your own countdown timer, as simply as:

public class CountDown {
    //Counts down from x to 0 in approximately
    //(little more than) s * x seconds. 
    static void countDown(int x, int s) {
        while (x > 0 ) { 
            System.out.println("x = " + x); 
            try {
                Thread.sleep(s*1000);
            } catch (Exception e) {}
            x--;
        }   
    }

    public static void main(String[] args) {
        countDown(5, 1); 
    }   
}

Or you could use Java Timer API




回答3:


It is simple to countdown with java..

      int minute=10,second=60; // 10 min countdown
      int delay = 1000; //milliseconds
  ActionListener taskPerformer = new ActionListener() {
  public void actionPerformed(ActionEvent evt) {
      second--;
      // do something with second and minute. put them where you want.
      if (second==0) {
          second=59;
          minute--;

          if (minute<0) {
              minute=9;
          }
      }
  }
};
new Timer(delay, taskPerformer).start();


来源:https://stackoverflow.com/questions/12556190/java-countdown-timer-without-gui

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!