简体   繁体   English

如何使用 func (Swift) 获取牛奶成本

[英]How can I get the milk cost using func (Swift)

I'm studying the Swift code now, and for the func, I have a question.我现在正在研究 Swift 代码,对于函数,我有一个问题。

The code is代码是

func getMilk(bottles: Int) {
    var cost = bottles * 1.5
    print("Milk cost is $\(cost)")
}

getMilk(bottles: 4)


    func getMilk(numberOfBottles: Int,
                 bottleVolume: Float) -> Float {
        var cost = Float(numberOfBottles) * bottleVolume
        return cost
    }
    print("Milk func return: \(getMilk(bottles: 5, bottleVolume: 1.5))")

This example will return the total volume of milt in bottles, given the number of bottles and each bottle's volume.这个例子将返回瓶子中 milt 的总体积,给定瓶子的数量和每个瓶子的体积。

Great job on deciding to learn Swift!决定学习 Swift 真是太棒了!

Please remember not to use magic numbers in your code.请记住不要在代码中使用幻数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM