我有一个UIPicker,用户输入一个指定的时间(即13:00,13:01,13:02等) - 这决定了他们的分数。一旦他们点击按钮,就会通过这个“if-else”声明确定的分数。一切似乎都在大部分时间都有效 - 但我正在发生一些不稳定的行为。这是代码:iPhone在'if'语句上产生奇怪的结果
//Gets my value from the UIPicker and then converts it into a format that can be used in the 'if' statement.
NSInteger runRow = [runTimePicker selectedRowInComponent:2];
NSString *runSelected = [runTimePickerData objectAtIndex:runRow];
NSString *runSelectedFixed = [runSelected stringByReplacingOccurrencesOfString:@":" withString:@"."];
//The actual 'if' statment.
if ([runSelectedFixed floatValue] <= 13.00) {
runScore = 100;
} else if ([runSelectedFixed floatValue] <= 13.06) {
runScore = 99;
} else if ([runSelectedFixed floatValue] <= 13.12) {
runScore = 97;
} else if ([runSelectedFixed floatValue] <= 13.18) {
runScore = 96;
} else if ([runSelectedFixed floatValue] <= 13.24) {
runScore = 94;
} else if ([runSelectedFixed floatValue] <= 13.30) {
runScore = 93;
} else if ([runSelectedFixed floatValue] <= 13.36) {
runScore = 92;
} else if ([runSelectedFixed floatValue] <= 13.42) {
runScore = 90;
} else if ([runSelectedFixed floatValue] <= 13.48) {
runScore = 89;
} else if ([runSelectedFixed floatValue] <= 13.54) {
runScore = 88;
}
现在,当我测试程序时,我会得到预期的结果,当我选择'13:00'这是'100'。当我选择'13:01和13:05'之间的所有时间时,我也会得到'99'的预期结果。但是,当我选择'13:06'时,它给了我'97'的分数。 '13:07到13:12'我也得到了'97'的分数 - 这是我们期望的结果。为什么我会在'13:12'上获得'97'的权利,但'13:06'上没有获得'99'的权利?这可能是内存泄漏或什么?
您是否尝试过记录'[runSelectedFixed floatValue]'?机会是你的浮动不*精确* 13.06,但略多...... – 2010-06-14 16:23:21