除以零它不是工作!如何修复在android
我用不同的方法来解决的情况下,但没有奏效
任何人谁可以帮我吗?
.............................................. ................................
case '/':
pp = Double.parseDouble(text.getText().toString());
if (sss == '/') {
if (Double.parseDouble(text.getText().toString()) == 0.0 ||Integer.parseInt(text.getText().toString()) == 0) {
text.setText("");
text2.setText("");
Toast.makeText(getBaseContext(), "Cannot divide by zero", Toast.LENGTH_SHORT).show();
} else if (pp != 0 || pp != 0.0) {
vis = tt/pp;
temp = (int) vis;
if (vis == temp) {
text.setText(Integer.toString(temp));
} else {
text.setText(Double.toString(vis));
}
vis = 0;
ash = 0;
break;
}
}
使用try catch块 – Tauqir
只是一个提示:使用**有意义的**变量名称! –
作为一个问题,'temp =(int)vis;如果(vis == temp){} ...'比较完全匹配的float和int是脆弱的。你应该检查它们是否足够接近。请参阅http://stackoverflow.com/questions/1161199/is-relational-comparison-between-int-and-float-directly-possible-in-c – headuck