下面的所有值都是双精度值,但 switch 需要整数值。有没有办法解决?
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;
您可能最好只使用 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;
....
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)