提问人:JenairoB 提问时间:3/16/2023 更新时间:3/16/2023 访问量:37
如何在变量中使用方程式和if语句?它不起作用
How can I use an equation and if statement inside of a variable? It's not working
问:
所以我打印了我的变量 recommendedCalories,我给它一个标准输入 0 来查看是否正在打印任何内容,因此它打印了 0。但是,对于我从上一个屏幕中获得的变量值,我应该得到不同的答案。我的方程式没有得到我想要的答案是怎么回事?
package com.example.optilife
import...
class WelcomeActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_welcome)
val userAge = intent.getIntExtra("EXTRA_AGE", 0)
val userHeight = intent.getIntExtra("EXTRA_HEIGHT", 0)
val userWeight = intent.getIntExtra("EXTRA_WEIGHT", 0)
val userGender = intent.getStringExtra("EXTRA_GENDER")
val userActivityLevel = intent.getStringExtra("EXTRA_ACTIVITYLEVEL")
var userWeightGoal = intent.getStringExtra("EXTRA_WEIGHTGOAL")
var userWeightGoalCalories = {
userWeightGoal = if (userWeightGoal == "Weight loss") {
"300"
} else if (userWeightGoal == "Maintain weight") {
"0"
} else {
"-300"
}
}
var recommendedCalories = 0
var recommendedCaloriesCalculator = {
recommendedCalories = if (userGender == "Male") {
(66 + 13.7userWeight + 5userHeight - 6.8userAge -
userWeightGoal.toString().toInt()).toString().toInt()
} else {
(655 + 9.6userWeight + 1.8userHeight - 4.7userAge -
userWeightGoal.toString().toInt()).toString().toInt()
}
}
findViewById<TextView>(R.id.tv_Test).apply {
text = recommendedCalories.toString()
}
}
}
答:
0赞
Enowneb
3/16/2023
#1
您已在此处创建了一个函数:recommendedCaloriesCalculator()
var recommendedCaloriesCalculator = {
recommendedCalories = ...
}
因此,如果您不显式调用 ,将永远不会被执行,因此不会更新。recommendedCaloriesCalculator()
recommendedCalories = ...
recommendedCalories
因此,您可以:
- 在设置 TextView 之前调用
recommendedCaloriesCalculator()
- 只需移除并留在那里
var recommendedCaloriesCalculator = { ... }
recommendedCalories = ...
选项 1:
package com.example.optilife
import...
class WelcomeActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_welcome)
val userAge = intent.getIntExtra("EXTRA_AGE", 0)
val userHeight = intent.getIntExtra("EXTRA_HEIGHT", 0)
val userWeight = intent.getIntExtra("EXTRA_WEIGHT", 0)
val userGender = intent.getStringExtra("EXTRA_GENDER")
val userActivityLevel = intent.getStringExtra("EXTRA_ACTIVITYLEVEL")
var userWeightGoal = intent.getStringExtra("EXTRA_WEIGHTGOAL")
var userWeightGoalCalories = {
userWeightGoal = if (userWeightGoal == "Weight loss") {
"300"
} else if (userWeightGoal == "Maintain weight") {
"0"
} else {
"-300"
}
}
var recommendedCalories = 0
// BTW, are you missing * for the following equations?
var recommendedCaloriesCalculator = {
recommendedCalories = if (userGender == "Male") {
(66 + 13.7userWeight + 5userHeight - 6.8userAge -
userWeightGoal.toString().toInt()).toString().toInt()
} else {
(655 + 9.6userWeight + 1.8userHeight - 4.7userAge -
userWeightGoal.toString().toInt()).toString().toInt()
}
}
// Call the function here to update recommendedCalories
recommendedCaloriesCalculator()
findViewById<TextView>(R.id.tv_Test).apply {
text = recommendedCalories.toString()
}
}
}
选项 2:
package com.example.optilife
import...
class WelcomeActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_welcome)
val userAge = intent.getIntExtra("EXTRA_AGE", 0)
val userHeight = intent.getIntExtra("EXTRA_HEIGHT", 0)
val userWeight = intent.getIntExtra("EXTRA_WEIGHT", 0)
val userGender = intent.getStringExtra("EXTRA_GENDER")
val userActivityLevel = intent.getStringExtra("EXTRA_ACTIVITYLEVEL")
var userWeightGoal = intent.getStringExtra("EXTRA_WEIGHTGOAL")
var userWeightGoalCalories = {
userWeightGoal = if (userWeightGoal == "Weight loss") {
"300"
} else if (userWeightGoal == "Maintain weight") {
"0"
} else {
"-300"
}
}
var recommendedCalories = 0
// The simplest solution is to remove recommendedCaloriesCalculator
// BTW, are you missing * for the following equations?
recommendedCalories = if (userGender == "Male") {
(66 + 13.7userWeight + 5userHeight - 6.8userAge - userWeightGoal.toString().toInt()).toString().toInt()
} else {
(655 + 9.6userWeight + 1.8userHeight - 4.7userAge - userWeightGoal.toString().toInt()).toString().toInt()
}
findViewById<TextView>(R.id.tv_Test).apply {
text = recommendedCalories.toString()
}
}
}
评论