尝试将 BMI(体重指数)计算为 Swift 中的应用程序。制作计算函数我找不到解决方案
@IBOutlet weak var height: UITextField!
@IBOutlet weak var weight: UITextField!
@IBAction func calculate(_ sender: UIButton) {
}
@IBAction func reset(_ sender: UIButton) {
}
func calculateIMC(){
var textHeight = height.text
var textWeight = weight.text
var intHeight:Int? = Int(textHeight!) ?? 0
var intWeight:Int? = Int(textWeight!) ?? 0
let calculateHeight: Int? = (intHeight * intHeight)
}
最后一行代码的错误信息:
二元运算符“*”不能应用于两个“Int?”操作数
最佳答案
问题在于毫无意义且错误的类型注释。删除它们!所有值都是非可选的(和常量)
func calculateIMC(){
let textHeight = height.text
let textWeight = weight.text
let intHeight = Int(textHeight!) ?? 0
let intWeight = Int(textWeight!) ?? 0
let calculateHeight = intHeight * intHeight // probably intHeight * intWeight
}
关于swift - 二元运算符 '*' 不能应用于两个 'Int?' 操作数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56538034/