arrays - BMI计算器总是快速返回0
问题描述
我一直在努力学习 swift 并决定构建一个小的 BMI 计算器只是为了习惯一切。我以为我已经弄清楚了一切,但由于某种原因,它每次都返回 0。我不太明白为什么。我觉得这可能与@State var
s 有关,但我不确定因为那些不适合textinput
s 的似乎只是找到了。
这就是我所拥有的:
import SwiftUI
struct ContentView: View {
@State var heightFeet: String = ""
@State var heightInches: String = ""
@State var weight: String = ""
@State var bmi: String = ""
@State var bmiText: String = ""
@State var bmiDetail: String = ""
var body: some View{
VStack {
VStack {
VStack {
Text("BMI Calculator")
.font(.largeTitle)
.padding(.top)
.foregroundColor(Color.white)
}
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: 100)
.background(Color.black)
.shadow(radius: 10)
VStack {
TextField("Enter height in feet", text: $heightFeet)
.padding(.all)
.frame(width: 300)
.textFieldStyle(RoundedBorderTextFieldStyle())
.background(Color.clear)
.foregroundColor(.black)
.shadow(radius: 5)
.keyboardType(.numberPad)
TextField("Enter height in inches", text: $heightInches)
.padding(.all)
.frame(width: 300)
.textFieldStyle(RoundedBorderTextFieldStyle())
.background(Color.clear)
.foregroundColor(.black)
.shadow(radius: 5)
.keyboardType(.numberPad)
TextField("Enter weight", text: $weight)
.padding(.all)
.frame(width: 300)
.textFieldStyle(RoundedBorderTextFieldStyle())
.background(Color.clear)
.foregroundColor(.black)
.shadow(radius: 5)
.keyboardType(.numberPad)
}
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
.background(Color.gray)
VStack {
Text(bmiText)
.font(.title)
.foregroundColor(Color.black)
Text(bmi)
.font(.largeTitle)
.foregroundColor(Color.black)
Text(bmiDetail)
.padding(.all)
.font(.headline)
.foregroundColor(.black)
}
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
.padding()
}
HStack {
Button(action: {
let heightInFeet = Int(self.heightFeet)
let heightInInches = Int(self.heightInches)
let weights = Int(self.weight)
let height = Int(heightInFeet! * 12 + heightInInches!)
let bmis = Int(weights! / (height * height) * 703)
self.bmi = "\(bmis)%"
self.bmiText = "Your BMI is"
if bmis > 30{
self.bmiDetail = "You are considered to be obese."
}
else if bmis > 25{
self.bmiDetail = "You are considered to be overweight."
}
else{
self.bmiDetail = "You are healthy!"
}
}) {
HStack {
Image(systemName: "plus.square")
Text("Calculate")
}
.frame(minWidth: 0, maxWidth: .infinity)
.font(.title)
.padding(.horizontal, 60.0)
.padding(.vertical, 20)
.background(Color("button"))
.cornerRadius(10)
.foregroundColor(Color.black)
.shadow(radius: 5)
.padding(.vertical, 40)
.padding(.horizontal, 20)
}
}
.background(Color.gray)
}
.frame(minWidth:0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
.background(Color.gray)
.edgesIgnoringSafeArea(.all)
.onTapGesture {
self.endTextEditing()
}
}
}
extension View {
func endTextEditing() {
UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder),
to: nil, from: nil, for: nil)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
解决方案
问题不在于代码,而在于您的计算。
let bmis = Int(weights! / (height * height) * 703)
例子:
重量 = 100 公斤 高度 = 76 英寸
结果 ==> 100 / (76 * 76) * 703 ==> 100 / 5776 * 703 ==> 100 / 4060528 ==> ~0
您可以将公式更改为
703*体重/身高*身高
得到正确的结果。
有关公式,请参见www.cdc.gov
推荐阅读
- jquery - 使用jquery通过不同的列搜索数据
- android - 如何使用 Retrofit 处理未知响应类型
- c# - 如何将 VideoFrame 或 ImageFeatureValue 调整为特定大小以匹配输入形状要求?
- soap - 在 IIB 中配置 SOAP 请求节点和 SOAP 输入节点的等待时间
- java - 如何运行有关 persistence.xml 的修复错误
- ios - 如何在 CAShapeLayer 上仅填充形状的一部分
- actionscript-3 - 加载文件名包含字符“%”的文件时如何修复错误
- javascript - 带有下拉国家代码vue js的电话号码不起作用?
- javascript - Mongoose- 使用 findOneAndUpdate 更新插入时出错
- mysql - 将 MySQL 查询结果转换为 JSON