下面的所有值都是双精度数,但是开关需要一个整数值。反正有吗?
switch(fivePercentValue){
case floor((5*fivePercentValue) / 100):
fivePercent_.backgroundColor = [UIColor greenColor];
fivePercentLabel_.textColor = [UIColor greenColor];
break;
case ceil((5*fivePercentValue) / 100):
fivePercent_.backgroundColor = [UIColor greenColor];
fivePercentLabel_.textColor = [UIColor greenColor];
break;
default:
fivePercent_.backgroundColor = [UIColor redColor];
fivePercentLabel_.textColor = [UIColor redColor];
break;
答案 0 :(得分:6)
你最好只使用if else并测试范围,但是你可以对你的FivePercentValue执行一些数学运算,然后将它转换为整数,以便不同的整数表示不同的范围,例如
switch( (int)(value*10.0) )
{
case 0: // this is 0.0 <= value < 0.1
break;
case 1: // this is 0.1 <= value < 0.2
break;
case 2: // this is 0.2 <= value < 0.3
break;
....
}