BMI计算器总是快速返回0

Posted

技术标签:

【中文标题】BMI计算器总是快速返回0【英文标题】:BMI Calculator always returns 0 in swift 【发布时间】: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) 【参考方案1】:

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

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

例子:

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

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

你可以把公式改成

703 * 体重/身高 * 身高

得到正确的结果。

公式见www.cdc.gov

【讨论】:

以上是关于BMI计算器总是快速返回0的主要内容,如果未能解决你的问题,请参考以下文章

《Python之BMI计算》

如何用Python计算BMI值?

体脂率怎么计算

Python3实现计算BMI指数,跟我一起来计算你的BMI吧

BMI体质指数计算器开发

Core Data 计算属性总是返回 0