【发布时间】:2021-05-11 09:17:14
【问题描述】:
当我一次调用该函数时,它运行良好,但如果我再次调用它,它将显示不同的值并读取不同的 else 块,请检查我附加整个类和驱动程序代码的代码以及以下函数的输出 我认为 if else 条件块中有错误我不明白为什么它在第二次调用函数时给出不同的值如果我再次运行它第三次它将给出正确的输出
enum ComponentCondition { missing, damaged, good }
class ComponentModel {
String name;
ComponentCondition condition;
double rate;
double componentPercent;
double kerbWeight;
double componentPrice;
int lifespan;
int age;
ComponentModel(
{this.name,
@required this.rate,
@required this.lifespan,
@required this.kerbWeight,
@required this.componentPercent,
@required this.age});
int calculatePrice() {
double weight = (componentPercent * kerbWeight) / 100;
age = DateTime.now().year - age;
double scrapValue = weight * rate;
int i = lifespan ~/ 3;
if ((condition == ComponentCondition.good ||
condition == ComponentCondition.damaged) &&
i == 0) {
componentPrice = scrapValue;
} else if (condition == ComponentCondition.good && age < lifespan) {
if (age >= 0 && age <= i) {
print('bestconditionPrice');
componentPrice = scrapValue * 3;
} else if (age > i && age <= i * 2) {
componentPrice = scrapValue * 2;
} else if (age > i * 2 && age < lifespan) {
componentPrice = scrapValue * 1.5;
}
} else if ((age >= lifespan) || condition == ComponentCondition.damaged) {
componentPrice = scrapValue;
print('why??????');
} else
componentPrice = 0;
return componentPrice.toInt();
}
void updateCondition(ComponentCondition partCondition) {
condition = partCondition;
}
}
void main(List<String> args) {
ComponentModel engine = ComponentModel(
componentPercent: 18, kerbWeight: 695, rate: 17, lifespan: 13, age: 2021);
engine.updateCondition(ComponentCondition.good);
int price = engine.calculatePrice();
int price2 = engine.calculatePrice();
print(' price1 :$price');
print("price2 : $price2");
}
【问题讨论】:
标签: flutter runtime-error calculation