【问题标题】:how do i add a number to an integer every second如何每秒将一个数字添加到一个整数
【发布时间】:2023-04-09 22:05:01
【问题描述】:

我正在制作一个 cookie clicker 之类的游戏,我想要一个每秒有一个特定数字的东西,比如说 5 被添加到另一个数字上。所以整数变量每秒增加 5。我将如何创建一种时间测量方法来测量时间,以便我可以将一个数字添加到另一个数字。

public class timeTesting {
    // I've put this method here for someone to create
    // a timer thing
    void timer()
    {

    }
    public static void main(String[] args) {        
        // Original number
        int number = 1;
        // Number I want added on to the original number every 5 seconds
        int addedNumber = 5;
    }
}

【问题讨论】:

  • 请尝试适当地格式化您的问题并使用正确的标点符号/语法。这样做会让你的问题更容易阅读和理解。

标签: java android time integer


【解决方案1】:

您可以使用Timer 安排TimerTask 在run() 方法中拥有所需的代码。检查下面的代码(run() 将在 5000 毫秒后调用一次):

Timer t = new Timer();
    t.schedule(new TimerTask() {
        @Override
        public void run() {
            number += addedNumber;
        }
    }, 5000);

您也可以使用 scheduleAtFixedRate(TimerTask task, long delay, long period) 来处理重复性任务(这里会立即调用 run,每隔 5000 毫秒):

Timer t = new Timer();
    t.scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {
            number += addedNumber;
        }
    }, 0, 5000);

【讨论】:

  • 我认为在您的答案中添加解释可能会更有用。
【解决方案2】:

如果您的目标是 android 平台,您可以使用 CountDownTimer,它允许您在一定时间内每隔一定时间执行一些代码。但请注意,android 不能像 J2SE 那样使​​用 main 方法。

无论如何,如果您希望编写一个 android 游戏,我强烈建议您从这里开始:Android Development

【讨论】:

  • 无论如何,在他的问题中,我看不到他在哪里要求倒计时,所以我认为他需要一个 Timer(或者更好的是 ScheduledExecutorService)
【解决方案3】:

我想建议开始学习RxJava。反应式编程对于游戏开发非常强大:https://www.youtube.com/watch?v=WKore-AkisY

使用 RxJava,您的问题可以通过 Observable interval() 方法解决:

https://github.com/Netflix/RxJava/wiki/Creating-Observables#interval

伊万

【讨论】:

    【解决方案4】:

    您应该考虑使用Timer,它会在某个时间间隔过后触发一个事件。它也可以每隔一段时间重复一次。

    【讨论】:

      【解决方案5】:

      不是很优雅,但可以工作的代码:

      public class MyTimer {
      
          private volatile int number;  //must be volatile as we're working on multiple threads.
          private final int numberToAdd;
          private final long timerTimeInMillis;
      
          public MyTimer() {
              number = 1;
              numberToAdd = 5;
              timerTimeInMillis = 5000;
          }
      
          public void runTimer() {
              new Thread() {                     //declaring a new anonymous Thread class
                  public void run() {            //that has to override run method.
                      while (true)               //run the program in an infinite loop
                      {
                          number += numberToAdd; //add a number
      
                          System.out.println("Added number. Now the number is: " + number);
                          try {
                              Thread.sleep(timerTimeInMillis);  //and then sleep for a given time.
                          } catch (InterruptedException e) {
                              e.printStackTrace();
                          }
                      }
                  }
              }.start();                         //and here we're starting this declared thread
          }
      
          public static void main(String[] args) 
          {
              new MyTimer().runTimer();
              try {
                  Thread.sleep(100000);          //this application will work for 100sec.
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
          }
      }
      

      使用 java.util.Timer 会更优雅,但在这里您可能会熟悉匿名类。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-09-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-12-20
        • 1970-01-01
        • 2011-08-30
        相关资源
        最近更新 更多