Button calc = (Button) findViewById(R.id.btnCalculate);
calc.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
EditText number1 = (EditText) findViewById(R.id.dnp);
EditText number2 = (EditText) findViewById(R.id.ponte);
EditText number3 = (EditText) findViewById(R.id.aro);
EditText number4 = (EditText) findViewById(R.id.ed);
EditText number5 = (EditText) findViewById(R.id.esf);
EditText number6 = (EditText) findViewById(R.id.curvint);
EditText number7 = (EditText) findViewById(R.id.curvb);
EditText number8 = (EditText) findViewById(R.id.ind);
EditText number9 = (EditText) findViewById(R.id.espmin);
TextView display = (TextView) findViewById(R.id.display);
double dnp = Double.parseDouble(number1.getText().toString());
double ponte = Double.parseDouble(number2.getText().toString());
double aro = Double.parseDouble(number3.getText().toString());
double ed = Double.parseDouble(number4.getText().toString());
double esf = Double.parseDouble(number5.getText().toString());
double curvint = Double.parseDouble(number6.getText().toString());
double curvb = Double.parseDouble(number7.getText().toString());
double ind = Double.parseDouble(number8.getText().toString());
double espmin = Double.parseDouble(number9.getText().toString());
double R1;
double R2;
double DT; //Decentração total
double DM; //Decentração monocular
double diametro;
double S1;
double S2;
double espessurafinal;
if (esf < 0)
{
R1 = (ind - 1 / curvb) * 1000;
R2 = (ind - 1 / curvint) * 1000;
DT = (aro+ponte)-dnp;
DM = (DT / 2);
diametro = (ed + 2 * DM) / 2;
S1 = R1 - (Math.sqrt(Math.exp(R1)-Math.exp(diametro)));
S2 = R2 - (Math.sqrt(Math.exp(R2)-Math.exp(diametro)));
espessurafinal = (S1 - S2) + espmin;
display.setText(espessurafinal + "" + "\nMilímetros");
}
else {
R1 = (ind - 1 / curvb) * 1000;
R2 = (ind - 1 / curvint) * 1000;
DT = (aro+ponte)-dnp;
DM = (DT / 2);
diametro = (ed + 2 * DM) / 2;
S1 = R1 - (Math.sqrt(Math.exp(R1)-Math.exp(diametro)));
S2 = R2 - (Math.sqrt(Math.exp(R2)-Math.exp(diametro)));
espessurafinal = espmin - (S1 + S2);
display.setText(espessurafinal + "\nMilímetros");
}
}
});
我可能在这里遗漏了一些非常微不足道的东西,我的编码技巧也是如此 非常有限,但是当我按下计算按钮时,此代码片段会返回&#34; Infinity&#34;现在,我之前收到NaN并设法摆脱它,但只是看到它变得无限!我研究并发现它是由于 有些数字除以0,但我不明白...... (哦,是的,我确保输入的所有数字都不是0)
可能只是我的编码把东西放在错误的地方? (是的,我知道我可以通过使用函数和所有这些来使代码变得更好......但我学习的人,对我很温柔大笑)。
或者,我的数学非常错误并且返回了极高的东西?
先谢谢大家!
答案 0 :(得分:1)
似乎某些输入实际上不是数字,或者是与某些随机字符串混合的数字。通过检查输入是否为Double的实例来对编辑文本值进行验证检查。 input instanceof Double
if( input instanceof Double) {
System.out.println("input is a Double");
}
在检查实例之前,请确保输入是干净的,只有数字。在编辑文本上运行java正则表达式^[0-9]*$
,如果为true则只输入数字,然后使用{{1} }或Double.valueOf(val)
唯一的区别是后者返回Double类型的对象,而另一个只返回一个基本类型。