【问题标题】:BMI Calculator always returns 0 in swiftBMI计算器总是快速返回0
【发布时间】:2020-03-09 19:34:59
【问题描述】:

我一直在努力学习 swift,并决定构建一个小型 BMI 计算器以适应一切。我以为我已经弄清楚了一切,但由于某种原因,它每次都返回 0。我不太明白为什么。我觉得这可能与 @State var s 有关,但是我不确定是否因为那些不适合 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()
    }
}

【问题讨论】:

  • 你不应该用整数来计算。或者至少先乘以703,然后再除以(height * height)

标签: arrays swift swiftui


【解决方案1】:

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

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

例子:

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

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

你可以把公式改成

703 * 体重/身高 * 身高

得到正确的结果。

公式见www.cdc.gov

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-08-16
    • 1970-01-01
    • 2021-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-04
    • 1970-01-01
    相关资源
    最近更新 更多