我有一个带有2个标签的相当简单的计算器,标签看起来工作正常,第一个标签计算应该如此,但第二个标签的计算不断抛出捕捉错误。 / p>
在第二个选项卡中,我有2个if语句,用于检查哪个edittext字段包含数据。具体取决于包含哪些数据的数据。
下面是我按下按钮时的代码我已经放在一起,它可能是精简的,但我只是一个不成熟的人,所以对我来说很容易。
有什么建议让这个工作吗?
calculate.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View view) {
if ( tabs.getCurrentTab() == 0) {
try {
if (d.getText().toString().trim().length() == 0) {
a1 = a.getText().toString();
double a2 = Double.parseDouble(a1);
b1 = b.getText().toString();
double b2 = Double.parseDouble(b1);
double sum = (a2 * b2) / 10000;
double total = Math.round((sum) * 10) / 10.0;
answer.setBackgroundColor(Color.parseColor("#d6ffffff"));
answer.setText(String.valueOf("Total Area Allocation = " + total + " ha"));
} else if (d.getText().toString().trim().length() >= 1) {
a1 = a.getText().toString();
double a2 = Double.parseDouble(a1);
b1 = b.getText().toString();
double b2 = Double.parseDouble(b1);
d1 = d.getText().toString();
double d2 = Double.parseDouble(d1);
double sum = (a2 * b2) / 10000;
double sum2 = (a2 * b2) / d2;
double total = Math.round((sum) * 10) / 10.0;
double total2 = Math.round((sum2) * 10) / 10.0;
answer.setBackgroundColor(Color.parseColor("#d6ffffff"));
answer.setText(String.valueOf("Total Area Allocation = " + total + " ha"));
answer2.setBackgroundColor(Color.parseColor("#d6ffffff"));
answer2.setText(String.valueOf("Set break " + total2 + " m"));
}
} catch (Exception e) {
answer.setBackgroundColor(Color.parseColor("#d6ffffff"));
answer.setText("Enter values in input fields");
}
}
if (tabs.getCurrentTab() == 1) {
try {
if (ff.getText().toString().trim().length() == 0) {
aa1 = aa.getText().toString();
double aa2 = Double.parseDouble(aa1);
bb1 = bb.getText().toString();
double bb2 = Double.parseDouble(bb1);
cc1 = cc.getText().toString();
double cc2 = Double.parseDouble(cc1);
dd1 = dd.getText().toString();
double dd2 = Double.parseDouble(dd1);
ee1 = ee.getText().toString();
double ee2 = Double.parseDouble(ee1);
if (ee.getText().toString().trim().length() == 0){
double sum = (aa2 * bb2)/(cc2 - dd2);
double total = Math.round((sum) * 10) / 10.0;
answer.setBackgroundColor(Color.parseColor("#d6ffffff"));
answer.setText(String.valueOf("Total Area Allocation = " + total + " ha"));
}
else if (cc.getText().toString().trim().length() == 0){
double sum = (aa2 * bb2)/ee2;
double total = Math.round((sum) * 10) / 10.0;
answer.setBackgroundColor(Color.parseColor("#d6ffffff"));
answer.setText(String.valueOf("Total Area Allocation = " + total + " ha"));
}
} else if (ff.getText().toString().trim().length() >= 1) {
aa1 = aa.getText().toString();
double aa2 = Double.parseDouble(aa1);
bb1 = bb.getText().toString();
double bb2 = Double.parseDouble(bb1);
cc1 = cc.getText().toString();
double cc2 = Double.parseDouble(cc1);
dd1 = dd.getText().toString();
double dd2 = Double.parseDouble(dd1);
ee1 = ee.getText().toString();
double ee2 = Double.parseDouble(ee1);
ff1 = ff.getText().toString();
double ff2 = Double.parseDouble(ff1);
if (ee.getText().toString().trim().length() == 0){
double sum = (aa2*bb2)/(cc2-dd2);
double sum2 = (sum * 10000) / ff2;
double total = Math.round((sum) * 10) / 10.0;
double total2 = Math.round((sum2) * 10) / 10.0;
answer.setBackgroundColor(Color.parseColor("#d6ffffff"));
answer.setText(String.valueOf("Total Area Allocation = " + total + " ha"));
answer2.setBackgroundColor(Color.parseColor("#d6ffffff"));
answer2.setText(String.valueOf("Set break " + total2 + " m"));
}
else if (cc.getText().toString().trim().length() == 0){
double sum = (aa2 * bb2)/ee2;
double sum2 = (sum * 10000) / ff2;
double total = Math.round((sum) * 10) / 10.0;
double total2 = Math.round((sum2) * 10) / 10.0;
answer.setBackgroundColor(Color.parseColor("#d6ffffff"));
answer.setText(String.valueOf("Total Area Allocation = " + total + " ha"));
answer2.setBackgroundColor(Color.parseColor("#d6ffffff"));
answer2.setText(String.valueOf("Set break " + total2 + " m"));
}
}
} catch (Exception e) {
answer.setBackgroundColor(Color.parseColor("#d6ffffff"));
answer.setText("Enter values in input fields 2");
}
}
}
});
答案 0 :(得分:1)
虽然并不严格地说明为什么会遇到异常问题,但我是否可以提出一些建议来帮助您改进代码,并希望能够减少问题。
public function(): void {
let token: string = localStorage.getItem('token');
provideAuth({
tokenName: 'token',
tokenGetter: token
});
}
和aa
实际上是什么。尝试解释它们实际包含的内容。 bb
或者你有什么。例如,创建用于处理每个选项卡计算的函数,并将该逻辑移到那里。
numOfCows
例如,在这里我创建了一个名为 calculate.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View view) {
if (tabs.getCurrentTab() == 0) {
calculateFirstTab();
} else if (tabs.getCurrentTab() == 1) {
calculateSecondTab();
}
}
});
的函数,它允许我传入TextView和一些文本。这将让您将背景颜色设置为一个位置,并更新文本。然后,创建另一个名为setAnswer
的函数,可以传递总数,现在可以像这样设置两个答案。
setAnswers
肯定有很多其他方法可以改进,但只有一些建议可以帮助你。