我是一名想学习 Swift 的设计师,而且我是初学者。
我没有任何经验。
我正尝试在 Xcode 的 playground 中使用基本代码创建一个小费计算器。
这是我目前所拥有的。
var billBeforeTax = 100
var taxPercentage = 0.12
var tax = billBeforeTax * taxPercentage
我得到错误:
Binary operator '*' cannot be applied to operands of type 'Int' and 'Double'
这是否意味着我不能乘 double ?
我是否遗漏了变量和 double 的任何基本概念?
最佳答案
同一数据类型只能有两个。
var billBeforeTax = 100 // Interpreted as an Integer
var taxPercentage = 0.12 // Interpreted as a Double
var tax = billBeforeTax * taxPercentage // Integer * Double = error
如果您像这样声明 billBeforeTax
..
var billBeforeTax = 100.0
它将被解释为 Double 并且乘法将起作用。或者您也可以执行以下操作。
var billBeforeTax = 100
var taxPercentage = 0.12
var tax = Double(billBeforeTax) * taxPercentage // Convert billBeforeTax to a double before multiplying.
关于swift - swift 乘以变量和 double ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30676752/