计算难题 - 如何影响另一个文本字段? [英] Calculation Conundrum - how do I influence another textfield?

查看:26
本文介绍了计算难题 - 如何影响另一个文本字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我有一个问题,当我的计算器应用程序中使用某些参数时 - 它会使结果不正确.问题是我有几个小时和分钟的单独文本字段,例如我有一个文本字段的开始时间13"和另一个文本字段的30",完成时间24"和00"在他们的相应的文本字段.答案应该是 10 小时 30 分钟,但我得到的答案是 11 小时 30 分钟.代码如下.

Basically I have a problem in that when certain parameters are used in my calculator app - it makes the result incorrect. The issue is that I have separate text fields for hours and minutes and say for example I have as the start time "13" in one text field and "30" in the other with the finish time "24" and "00" in their respective text fields. The answer should be 10 hours 30 minutes, but the answer I get is 11 hours 30 minutes. The code for this is the following.

 -(IBAction)done:(id)sender {
  int result = [finishHours.text intValue] - [startHours.text intValue];
  totalHours.text = [NSString stringWithFormat:@"%d", result];
  if (result < 0) { 
    totalHours.text = [NSString stringWithFormat:@"%d", result + 24];
  }



    -(IBAction)done2:(id)sender {
  int result = [startMinutes.text intValue] - [finishMinutes.text intValue];
  totalMinutes.text = [NSString stringWithFormat:@"%d", result];
  if (result < 0) {
    totalMinutes.text = [NSString stringWithFormat:@"%d", result + 60];
  }
}

我想这样做,如果满足某些参数,则 totalHours.text 减少 1 小时以反映总分钟数.我将如何在代码中进行该计算?

I want to make it so if certain parameters are met, then the totalHours.text is reduced by 1 hour to reflect the total minutes. How would I go about that calculation in code?

谢谢!

推荐答案

您应该首先将开始时间和结束时间转换为单个单位(分钟),减去分钟,然后转换回小时:分钟.

You should first convert the start and finish times to a single unit (minutes), subtract the minutes, then convert back to hours:minutes.

例如,将 startTotalMinutes 计算为 startHours*60 + startMinutes.对完成时间做同样的事情.从finishTotalMinutes 中减去startTotalMinutes.补偿低于零的结果(我认为只需添加 1440).然后将该结果转换回小时:分钟.对于小时,做结果 mod 60,余数是结果分钟.

For example, calc startTotalMinutes as startHours*60 + startMinutes. Do the same with the finish time. Subtract startTotalMinutes from finishTotalMinutes. Compensate for result lower than zero (I think just add 1440). Then convert that result back to hours:minutes. For the hours, do result mod 60 and the remainder is the result minutes.

以下是请求的代码示例.另请注意,小时数是结果除以 60,分钟 是结果模数 60.

Below is a code example as requested. Also note correction that the hours are result divided by 60 and the minutes are result mod 60.

- (int)hoursMinutesTextToMinutes:(NSString *)hoursText minutesText:(NSString *)minutesText
{
    return [hoursText intValue] * 60 + [minutesText intValue];
}

-(IBAction)done:(id)sender
{
    int startTotalMinutes = [self hoursMinutesTextToMinutes:startHours.text minutesText:startMinutes.text];
    int finishTotalMinutes = [self hoursMinutesTextToMinutes:finishHours.text minutesText:finishMinutes.text];

    int difference = finishTotalMinutes - startTotalMinutes;
    if (difference < 0)
    {
        difference += 1440;
    }

    int diffHours = difference / 60;
    int diffMinutes = difference % 60;

    totalHours.text = [NSString stringWithFormat:@"%d", diffHours];
    totalMinutes.text = [NSString stringWithFormat:@"%d", diffMinutes];
}

hoursMinutesTextToMinutes 是一个辅助方法,它返回 hours:minutes 文本作为一个minutes int.这避免了重复该代码.

The hoursMinutesTextToMinutes is a helper method that returns hours:minutes text as just a minutes int. This avoids duplicating that code.

只需要一个按钮操作(我用的是完成").

Only one button action is needed (I used "done").

请注意,这不会对输入的文本进行任何验证.您应该添加验证或使用 UIDatePicker 让用户指定开始/结束时间.

Please note this does not do any validation on the text entered. You should add validation or use a UIDatePicker to let user specify the start/finish times.

这篇关于计算难题 - 如何影响另一个文本字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆