seconds/minutes/hours 如何将整数拆分为字符串并自动转换

How to split an integer into String and Convert it automatically by seconds/minutes/hours

我已经在 GUI 上设置并显示了一个计时器。

我想让程序节省时间并加载它。我成功地做到了,但是, 我想在程序启动的时候加载之前的时间

ms 是毫秒,所以如果它超过 1000,它会将其转换为 1 秒并再次获得 0 的值。我已经创建了第二个 (millisecondTimer) 作为 (score) 来显示而不是将其更改为 0。在计时器停止之前,分数不会自行重置。

我想抓取分数并按顺序提取以获取以下值:

分钟//毫秒.

我试过提取它或将它除以不同的数字,但对我来说太难了:/

简单地说,我想自动检测乐谱的长度,并在字符串上获取分钟、秒和毫秒,并在 JLabel 之后显示它。

我可以创建其他整数,如 milliBackupsecondsBackupminuteBackup。 并将它们分别传递给 miliseconds/seconds/minutes。但如果可以的话,我想这样做。

public void beginTimer() {

            score++;
            ms++;

            if(ms==1000) {

                ms = 0;
                s++;

                if(s>59) {

                    s = 0;
                    m++;

                    if(m>59) {

                        timer.cancel();

                    }
                }

            }

            lblTimer.setText(displayTimer());

        }

并且 DisplayTimer 具有:

public String displayTimer() {
            return String.format("%02d:%02d:%03d", m, s, ms);
        }

你没有说你是如何更新方法的。如果您打电话给 Thread.sleep,则应格外小心。有更好的方法。但是使用您的代码:

// bad use of static but once you'll get it working, change it
static long s = 1;
static long m = 1;
static long ms = 1;

// the method is not beginTimer but updateTimer and goes inside a
// loop or something which calls it agan and again
public void updateTimer() {

        if(ms==1000L) {                  
            ms = 0;
            s++;  

            if(s==60L) {                      
                s = 0;
                m++; 

                if(m==60L) {                          
                    timer.cancel();                         
                }
            }                   
        }               
        lblTimer.setText(displayTimer());               
    }

如何根据 score 计算分钟 (m)、秒 (s) 和毫秒 (ms):

ms = score; 
s = ms / 1000; // integer div
ms = ms % 1000; // remainder
m = s / 60; // integer div
s = s % 60; // remainder