【问题标题】:Modulo time to get seconds, minutes, hours etc模时间得到秒,分钟,小时等
【发布时间】:2011-08-12 01:33:47
【问题描述】:

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

这样显示:

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 // ??
}

【问题讨论】:

  • 在我看来,这个问题与Objective-C几乎没有关系。我能看到的唯一 Objective-C 是与倒计时问题无关的方法定义。

标签: objective-c math time modulo


【解决方案1】:

好吧,如果% 10 为您提供了高达9 的值,您认为您需要对什么取模才能获得高达5 的值?

【讨论】:

    【解决方案2】:

    我不了解 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 是正数并且您正在倒计时。如果您真的希望它为负数并按照您的指示进行计数,请更改为:

        seconds++;
    
        _secs = abs(seconds) % 60;
    
    ...
    
        _mins = abs(seconds) / 60;
    
    ...

    【讨论】:

      【解决方案3】:
       _secs = seconds % 10;
       _tensecs = (seconds / 10) % 6;
       _mins = (seconds / 60) % 10;
       _tenmins = (seconds / 600) % 6;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-02-09
        • 1970-01-01
        • 1970-01-01
        • 2015-03-23
        • 1970-01-01
        • 2017-03-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多