对时间取模以获得秒、分钟、小时等

发布于 2024-11-29 10:54:19 字数 396 浏览 0 评论 0原文

我正在尝试构建一个倒计时,它有四个部分:

显示如下:

0 0 : 0 0

十分钟 分钟 : 十秒 秒

我已经让 _secs 在下面工作,_tensecs 上升到 9,这不好。

int seconds;
int _secs;
int _tensecs;
int _mins;
int _tenmins;

-(void) tick: (ccTime) dt
{
    seconds++; 

    _secs = seconds % 10;
    _tensecs = (seconds / 10) % 10;  // wrong
    _mins = seconds // ??
    _m_tenminsins = seconds // ??
}

Im trying to build a countdown, which has four sections:

to be displayed like this:

0 0 : 0 0

tenminutes minutes : tenseconds seconds

Ive got the _secs working below, the _tensecs goes up to 9 which is no good.

int seconds;
int _secs;
int _tensecs;
int _mins;
int _tenmins;

-(void) tick: (ccTime) dt
{
    seconds++; 

    _secs = seconds % 10;
    _tensecs = (seconds / 10) % 10;  // wrong
    _mins = seconds // ??
    _m_tenminsins = seconds // ??
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

花间憩 2024-12-06 10:54:19

好吧,如果 % 10 给出的值高达 9,您认为需要对什么取模才能获得高达 5 的值代码>?

Well, if % 10 gives you values that go up to 9, what do you think you need to modulo by to get values that go up to 5?

月下伊人醉 2024-12-06 10:54:19

我不了解 Objective-C,但这应该可以满足您的要求:

int seconds;
int _secs;
int _tensecs;
int _mins;
int _tenmins;

-(void) tick: (ccTime) dt
{
    seconds--; 

    _secs = seconds % 60;
    _tensecs = _secs / 10;
    _secs = _secs % 10;

    _mins = seconds / 60;
    _tenmins = _mins / 10;
    _mins = _mins % 10;
}

假设秒数为正并且您正在倒计时。如果您确实希望它为负数并向上计数,正如您似乎所表明的那样,请更改为:

    seconds++;

    _secs = abs(seconds) % 60;

...

    _mins = abs(seconds) / 60;

...

I don't know Objective-C, but this should do what you want:

int seconds;
int _secs;
int _tensecs;
int _mins;
int _tenmins;

-(void) tick: (ccTime) dt
{
    seconds--; 

    _secs = seconds % 60;
    _tensecs = _secs / 10;
    _secs = _secs % 10;

    _mins = seconds / 60;
    _tenmins = _mins / 10;
    _mins = _mins % 10;
}

This assumes seconds is positive and you're counting down. If you really want it negative and counting up, as you seem to be indicating, then change to:

    seconds++;

    _secs = abs(seconds) % 60;

...

    _mins = abs(seconds) / 60;

...
下壹個目標 2024-12-06 10:54:19
 _secs = seconds % 10;
 _tensecs = (seconds / 10) % 6;
 _mins = (seconds / 60) % 10;
 _tenmins = (seconds / 600) % 6;
 _secs = seconds % 10;
 _tensecs = (seconds / 10) % 6;
 _mins = (seconds / 60) % 10;
 _tenmins = (seconds / 600) % 6;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文