首页 > 解决方案 > BMI计算器总是快速返回0

问题描述

我一直在努力学习 swift 并决定构建一个小的 BMI 计算器只是为了习惯一切。我以为我已经弄清楚了一切,但由于某种原因,它每次都返回 0。我不太明白为什么。我觉得这可能与@State vars 有关,但我不确定因为那些不适合textinputs 的似乎只是找到了。

这就是我所拥有的:

 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()
    }
}

标签: arraysswiftswiftui

解决方案


问题不在于代码,而在于您的计算。

let bmis = Int(weights! / (height * height) * 703)

例子:

重量 = 100 公斤 高度 = 76 英寸

结果 ==> 100 / (76 * 76) * 703 ==> 100 / 5776 * 703 ==> 100 / 4060528 ==> ~0

您可以将公式更改为

703*体重/身高*身高

得到正确的结果。

有关公式,请参见www.cdc.gov


推荐阅读