问题
All of the values below are doubles, yet switch requires a integer value. Is there anyway around this?
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;
回答1:
You are probable better of just using if else and testing for ranges but you can perform some maths on you fivePercentValue and then convert it to a integer so that different integers represent different ranges for example
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;
....
}
来源:https://stackoverflow.com/questions/12398719/using-double-in-switch-statement