android - 为什么在 if 语句中取不到值?
问题描述
Widget build(BuildContext context) {
final bmi = weight / ((height / 100) * (height / 100));
final bmi = weight / ((height / 100) * (height / 100));
if(sex == 1){
final base = 66.47+(13.75*weight)+(5*height)-(6.76*age);
if(exercise == 0){
final active = base * 0.2;
}else if(exercise <= 3){
final active = base * 0.375;
}else if(exercise <= 5){
final active = base * 0.555;
}else{
final active = base * 0.8;
}
}else if(sex == 2){
final base = 65.51+(9.56*weight)+(1.85*height)-(4.68*age);
if(exercise == 0){
final active = base * 0.2;
}else if(exercise <= 3){
final active = base * 0.375;
}else if(exercise <= 5){
final active = base * 0.555;
}else if(exercise >= 6){
final active = base * 0.8;
}
}
return Scaffold(
appBar: AppBar(title: Text('Result')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
_calcBmi(bmi),
style: TextStyle(fontSize: 30),
),
SizedBox(
height: 16,
),
_buildIcon(bmi),
SizedBox(
height: 10,
),
Text(
_calcBase(base),
style: TextStyle(fontSize: 15),
),
SizedBox(
height: 10,
),
Text(
_calcActive(active),
style: TextStyle(fontSize: 15),
),
],
),
),
);
}
String _calcBmi(double bmi) {
var result = 'underweight';
if (bmi >= 35) {
result = 'severe obesity';
} else if (bmi >= 30) {
result = 'two-stage obesity';
} else if (bmi >= 25) {
result = 'first-stage obesity';
} else if (bmi >= 23) {
result = 'overweight';
}else if(bmi>=18.5){
result='normal';
}
return result;
}
String _calcBase(double base) {
var result = 'basic metabolic rate : $base';
return result;
}
String _calcActive(double active) {
var result = 'active metabolic rate : $active';
return result;
}
我正在尝试制作一个颤振的应用程序,输出 bmi 值,但不输出基本值和活动值。我想知道为什么我不能带来基础和积极的价值。我想根据输入的性别值和运动值以不同的方式打印出基本和活性代谢物。我不能在条件语句中得到最终值吗?
解决方案
您在 if 范围内声明 base 和 active,它在外部不存在。
尝试:
Widget build(BuildContext context) {
final bmi = weight / ((height / 100) * (height / 100));
final bmi = weight / ((height / 100) * (height / 100));
var base, active;
if(sex == 1){
然后在基础和活动之前删除每个决赛。
推荐阅读
- php - 使用值数组替换数组键
- python-3.x - 替换存储在字典列表中的字符串
- html - materializecss 的透明导航栏
- java - 如何将 InputStream 用于 RandomAccessFile 的一部分?
- c++ - 使用 C++ CommonAPI 编写服务器时,服务器进程中创建了多少个线程?
- java - 如何比较 2 LinkedHashMap 并获得键和值的差异?
- java - 使用 java io 从远程机器读取文件
- c# - CurrentDirectory 和 Excel-DNA
- javascript - 使用 jquery 更新图像源无法正常工作
- angular - Angular 编译器需要 TypeScript >=2.7.2 和 <2.8.0 但找到的是 2.8.3