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开发者_JAVA百科 < 0) {
result = result * -1;
}
You set totalHours.text before you change the sign of your result variable.
what do you mean it doesn't have any effect? is it because you have changed result after you have set totalHours? would this fix your issue?
-(IBAction)done:(id)sender {
int result = [finishHours.text intValue] - [startHours.text intValue];
if (result < 0) {
result = result * -1;
}
totalHours.text = [NSString stringWithFormat:@"%i", result];
精彩评论