在 Switch 语句中使用 Double [英] Using Double in Switch Statement

查看:102
本文介绍了在 Switch 语句中使用 Double的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下所有值都是双精度值,但 switch 需要一个整数值.反正有这个吗?

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;

推荐答案

您可能最好只使用 if else 并测试范围,但您可以对您的 FivePercentValue 执行一些数学运算,然后将其转换为整数,以便不同的整数例如表示不同的范围

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;
    ....
}

这篇关于在 Switch 语句中使用 Double的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆