Kotlin 中的 BMI 计算器

BMI Calculator in Kotlin

你好,我是 Kotlin 的新手,我正在尝试使用 android Studio 构建一个 BMI 计算器。我试图让它变得简单,所以通过接受用户输入,然后计算它 return 他们的 BMI 数字,然后是他们的状态(正常、超重、肥胖)。

将使用 3 个按钮: 计算 - 将根据输入的结果计算用户 BMI 清除 - 清除屏幕以计算新的输入 心率 - 现在将是一个非活动按钮。

目前,当我按下计算按钮时,应用程序会立即关闭,并且不会 return BMI 状态或 BMI 数字。我需要它来显示“你的 BMI 是 'bmi#' 你是 'obese, normal, or overweight'。但是 none 这种情况发生了,应用程序就崩溃了。这是我的代码示例


    class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val height = findViewById<EditText>(R.id.heightEditText)
        val weight = findViewById<EditText>(R.id.weightEditText)

        val calcButton = findViewById<Button>(R.id.button)
        val clearButton = findViewById<Button>(R.id.clearButton)
        val bmiInfo = findViewById<TextView>(R.id.infoBMITextView)
        val heartRateButton = findViewById<Button>(R.id.heartRateButton)


        // make heart rate button inactive
        heartRateButton.visibility = GONE

        // Make info text invisible until Calculate button is pressed
        bmiInfo.visibility = View.INVISIBLE

        calcButton.setOnClickListener{
            var heightValue = 0.0
            var weightValue = 0.0
            if(height.text.toString().isNotEmpty()){
                heightValue = height.text.toString().toDouble()
            }
            if(weight.text.toString().isNotEmpty()){
                weightValue = weight.text.toString().toDouble()
            }
            if(weightValue > 0.0 && heightValue > 0.0){
                val bmiValue = String.format("%0.2f", 
                weightValue*703/heightValue*heightValue)
                bmiInfo.text = "BMI is ${String.format("%0.2f",bmiValue)} you are 
                ${bmiResults(weightValue*703/heightValue*heightValue)}"
                bmiInfo.visibility = VISIBLE
            }
            else {
                Toast.makeText(
                    this, "Please input Weight and Height Values greater than 0",
                    Toast.LENGTH_LONG).show()
            }
        }

        clearButton.setOnClickListener {
            weight.text.clear()
            height.text.clear()
        }

    }
    

这是我为了在覆盖乐趣之外执行 BMI 计算而创建的函数


    fun bmiResults(bmi:Double):String{
        lateinit var answer: String
        if(bmi<18.5){
            answer="Underweight"
        } else if(bmi > 18.5 && bmi < 24.9) {
            answer="Normal"
        } else if(bmi > 24.9 && bmi < 30) {
            answer="Overweight"
        } else {
            answer="Obese"
        }
        return answer
    }
    

这是我的 activity_main 代码,以防有助于清理我的变量赋值,这可能会导致我不知道的问题。


    <TextView
        android:id="@+id/weightTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:text="Weight"
        android:textColor="#3F51B5"
        android:textColorHighlight="#FAF6F6"
        android:textColorLink="#3F51B5"
        android:textSize="20sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/heightEditText" />

    <EditText
        android:id="@+id/weightEditText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:ems="10"
        android:gravity="center"
        android:hint="in LBS"
        android:inputType="numberDecimal"
        android:minHeight="48dp"
        android:textColor="#3F51B5"
        android:textColorHighlight="#D6991E"
        android:textColorLink="#D6991E"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/weightTextView" />

    <TextView
        android:id="@+id/heightTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:text="Height"
        android:textColor="#3F51B5"
        android:textColorHighlight="#FAF6F6"
        android:textColorLink="#3F51B5"
        android:textSize="20sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.492"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/heightEditText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:ems="10"
        android:gravity="center"
        android:hint="in inches"
        android:inputType="numberDecimal"
        android:minHeight="48dp"
        android:textColor="#3F51B5"
        android:textColorHighlight="#D6991E"
        android:textColorLink="#D6991E"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.497"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/heightTextView" />

    <Button
        android:id="@+id/button"
        android:layout_width="158dp"
        android:layout_height="54dp"
        android:layout_marginStart="32dp"
        android:layout_marginLeft="32dp"
        android:layout_marginTop="44dp"
        android:text="Calculate"
        android:textColorHighlight="#F6F0F0"
        android:textColorLink="#3F51B5"
        android:textSize="16sp"
        android:textStyle="bold"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/infoBMITextView"
        tools:ignore="UnknownId" />

    <TextView
        android:id="@+id/infoBMITextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp"
        android:text="INFO"
        android:textColor="#3F51B5"
        android:textColorHighlight="#FAF6F6"
        android:textColorLink="#3F51B5"
        android:textSize="24sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/weightEditText" />

    <Button
        android:id="@+id/clearButton"
        android:layout_width="146dp"
        android:layout_height="52dp"
        android:layout_marginTop="341dp"
        android:text="Clear"
        android:textColorHighlight="#F6F0F0"
        android:textColorLink="#3F51B5"
        android:textSize="16sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/button"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/heartRateButton"
        android:layout_width="347dp"
        android:layout_height="67dp"
        android:layout_marginTop="80dp"
        android:text="Click for heart rate"
        android:textSize="18sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.515"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/clearButton" />
</androidx.constraintlayout.widget.ConstraintLayout>
    

第一次使用 Whosebug 很抱歉,如果问题不是我想的那样,我尝试提供所有必要的信息。谢谢!

您的代码中的错误在于您没有使用适当的格式进行转换的字符串格式转换̣

计算 bmiValue 而不是:

val bmiValue = String.format("%0.2f", 
            weightValue*703/heightValue*heightValue)

使用以下格式:

 val bmiValue = String.format(" %.2f",
                    weightValue*703/heightValue*heightValue)

此外,在将 bmiValue 和 bmiResult 设置为 textView 时,您需要执行相同的操作,并且还必须将 bmiValue 从 String 转换为 Double 以再次格式化,我认为这不需要,我已经完成了所需的转换假设您可能有相同的用例

所以,不要按以下方式设置文本:

bmiInfo.text = "BMI is ${String.format("%0.2f",bmiValue)} you are + "${bmiResults(weightValue*703/heightValue*heightValue)}"

按以下方式进行:

  val bmiDouble = bmiValue.toDouble()
  bmiInfo.text = "BMI is ${String.format("%.2f",bmiDouble)} you are" +  
                     "${bmiResults(weightValue*703/heightValue*heightValue)}"