1

Basically I've been trying to make a working hours calculator and I've run into a problem. When the start time's value is greater than the finish time (eg. start is 23 and finish is 19), the result comes up as a negative. So what I want it to do in that scenario is to then multiply the negative number by -1 to make it positive again. However, this code below doesn't seem to have any effect in my app.

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

    }
4

2 回答 2

4

在更改结果变量的符号之前设置 totalHours.text 。

于 2010-02-22T10:16:26.780 回答
1

你是什​​么意思它没有任何影响?是因为您在设置 totalHours 后更改了结果吗?这会解决你的问题吗?

-(IBAction)done:(id)sender {
int result = [finishHours.text intValue] - [startHours.text intValue];
if (result < 0) {
        result = result * -1;

}
totalHours.text = [NSString stringWithFormat:@"%i", result];
于 2010-02-22T10:16:31.317 回答