美文网首页
Swift-控制流 if else

Swift-控制流 if else

作者: XTK_iOS | 来源:发表于2019-05-13 13:48 被阅读0次
/// 占卜函数
///
/// - Parameters:
///   - girlName: 女孩名字
///   - boyName: 男孩名字
/// - Returns: 返回他们的相爱匹配度
func love(girlName: String, boyName: String)->String {
    let num = Int.random(in: 0...100)
    if num > 80 {
        return "匹配度:\(num)%,祝福你们,百年好合!"
    }else if num > 60 && num <= 80 {
        return "匹配度:\(num)%,请加油努力!"
    }else {
        return "匹配度:\(num)%,你们不合适!"
    }
}

//调用函数
let results = love(girlName: "Swift", boyName: "iOS")
print(results)// 打印结果 匹配度:26%,你们不合适!  匹配度:67%,请加油努力!
/// 计算衡量人体胖瘦程度
///
/// - Parameters:
///   - height: 身高
///   - weight: 体重
/// - Returns: 返回健康程度
func calculateBMI(height: Double, weight: Double)->String {
    //BMI 计算法: 体重指数 BMI = 体重(千克) / 身高(米)的平方即 kg/m2
    //round 四舍五入  pow:几次方
    let bmi = round(weight / (pow(height, 2)))
    var message = ""
    
    if bmi >= 40.0 {
        message = "Ⅲ度肥胖"
    }else if 30.0 <= bmi && bmi < 40.0 {
        message = "II度肥胖"
    }else if 28.0 <= bmi && bmi < 30.0 {
        message = "I度肥胖"
    }else if 24.0 <= bmi && bmi < 28.0 {
        message = "肥胖前期"
    }else if bmi >= 24 {
        message = "体重超重"
    }else if 18.5 <= bmi && bmi < 24.0 {
        message = "体重正常范围"
    }else {
        message = "体重过低"
    }
    return "你的BMI值是:\(bmi),\(message)"
}

//调用打印函数
print(calculateBMI(height: 1.75, weight: 60.6))

相关文章

网友评论

      本文标题:Swift-控制流 if else

      本文链接:https://www.haomeiwen.com/subject/nidlaqtx.html