BMI计算器在Kotlin



你好,我是Kotlin的新手,我正在尝试使用android Studio构建BMI计算器。我试图使它简单,所以通过采取用户输入,然后将计算并返回他们的BMI数字,然后是他们的状态(正常,超重,肥胖)。

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

目前,当我按下计算按钮,应用程序立即关闭,不返回BMI状态和BMI数字。我需要它显示"你的BMI是BMI #",你是"肥胖、正常或超重"。但这些都没有发生,应用程序只是崩溃。下面是我的代码示例


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

这是我为了在override fun

之外执行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>

第一次使用stackoverflow所以抱歉,如果问题不是我试图提供所有必要的信息的方式。谢谢!

代码中的错误在于字符串格式转换,您没有使用适当的格式来转换它

计算bmiValue而不是:

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

使用以下格式:

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

另外,在将bmiValue和bmiResult设置为textView时,您需要做同样的事情,并且还必须将bmiValue从字符串转换为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)}"
val bmi = weight.toFloat() / ((height.toFloat()/100)*(height.toFloat()/100))
val digit2format = String.format("%.2f", bmi).toFloat()

最新更新