I am trying to create a BMI calculator that takes the user's height and weight as inputs then calculates their BMI; from there, it uses a series of if-else statements to return a message to the user that states if they are healthy/over/underweight. I have no problem getting the program to return the calculated BMI value; but when I incorporate the if-else statement, I get the "Cannot convert return expression of type 'String' to return type 'Float'" error.
我正在尝试创建一个BMI计算器,将用户的身高和体重作为输入,然后计算他们的BMI;从那里开始,它使用一系列if-else语句向用户返回一条消息,告知用户是否健康/过度/不足。我没有问题让程序返回计算出的BMI值;但是当我合并if-else语句时,我得到“无法转换类型'String'的返回表达式以返回类型'Float'”错误。
Here's my code (I've only done one if-else statement so far):
这是我的代码(到目前为止我只做了一个if-else语句):
import UIKit
func bodyMassIndex (userHeight : Float, userWeight : Float) -> String {
let userHeightSquared = (userHeight*userHeight)
let userWeight = userWeight
let userBMI = (userWeight/userHeightSquared)
return userBMI
if userBMI > 25 {
return "Overweight"
}
}
print(bodyMassIndex (userHeight : 1.82, userWeight: 90.7))
2 个解决方案
#1
0
You just have to wrap the Float
value of userBMI
into a String
.
您只需将userBMI的Float值包装到String中即可。
func bodyMassIndex (userHeight : Float, userWeight : Float) -> String {
let userHeightSquared = (userHeight*userHeight)
let userWeight = userWeight
let userBMI = (userWeight/userHeightSquared)
return String(userBMI)
if userBMI > 25 {
return "Overweight"
}
}
#2
1
Your logic looks a little confused here:
你的逻辑看起来有点困惑:
return userBMI
if userBMI > 25 {
return "Overweight"
}
What is your function supposed to do? Return a BMI number, or a descriptive String? (It's trying to do both, and the compiler isn't happy)
你的功能应该是什么?返回BMI号码或描述性字符串? (它试图做到这两点,并且编译器不满意)
If I were a user, I'd want to see both the number and a statement about what it means - so you'd need two functions:
如果我是一个用户,我想看到数字和声明它的意思 - 所以你需要两个功能:
func bodyMassIndex (userHeight : Float, userWeight : Float) -> String {
let userHeightSquared = (userHeight*userHeight)
let userWeight = userWeight
let userBMI = (userWeight/userHeightSquared)
return userBMI
}
func bodyMassDiagnosis (bodyMassIndex : Float) {
if userBMI > 25 {
return "Overweight"
}
return "You're doing fine"
}
And then:
接着:
let bodyMassIndex = bodyMassIndex(height, weight)
let diagnosis = bodyMassDiagnosis(bodyMassIndex)
self.indexLabel.text = "BMI \(bodyMassIndex)"
self.diagnosisLabel.text = diagnosis
#1
0
You just have to wrap the Float
value of userBMI
into a String
.
您只需将userBMI的Float值包装到String中即可。
func bodyMassIndex (userHeight : Float, userWeight : Float) -> String {
let userHeightSquared = (userHeight*userHeight)
let userWeight = userWeight
let userBMI = (userWeight/userHeightSquared)
return String(userBMI)
if userBMI > 25 {
return "Overweight"
}
}
#2
1
Your logic looks a little confused here:
你的逻辑看起来有点困惑:
return userBMI
if userBMI > 25 {
return "Overweight"
}
What is your function supposed to do? Return a BMI number, or a descriptive String? (It's trying to do both, and the compiler isn't happy)
你的功能应该是什么?返回BMI号码或描述性字符串? (它试图做到这两点,并且编译器不满意)
If I were a user, I'd want to see both the number and a statement about what it means - so you'd need two functions:
如果我是一个用户,我想看到数字和声明它的意思 - 所以你需要两个功能:
func bodyMassIndex (userHeight : Float, userWeight : Float) -> String {
let userHeightSquared = (userHeight*userHeight)
let userWeight = userWeight
let userBMI = (userWeight/userHeightSquared)
return userBMI
}
func bodyMassDiagnosis (bodyMassIndex : Float) {
if userBMI > 25 {
return "Overweight"
}
return "You're doing fine"
}
And then:
接着:
let bodyMassIndex = bodyMassIndex(height, weight)
let diagnosis = bodyMassDiagnosis(bodyMassIndex)
self.indexLabel.text = "BMI \(bodyMassIndex)"
self.diagnosisLabel.text = diagnosis