gpt4 book ai didi

objective-c - 在 Switch 语句中使用 Double

转载 作者:塔克拉玛干 更新时间:2023-11-01 19:11:25 25 4
gpt4 key购买 nike

以下所有值都是 double 值,但 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;
....
}

关于objective-c - 在 Switch 语句中使用 Double,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12398719/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com