我有一个方程;
(((nmed - nsub) / (nmed + nsub))^2) *100
我查看了其他问题,但似乎无法理解如何在这个方程中实现 pow() 。
private String updateTotal() {
float nsub;
float nmed;
if(n_sub.getText().toString() != "" && n_sub.getText().length() > 0) {
nsub = Float.parseFloat(n_sub.getText().toString());
} else {
nsub = 0;
}
if(n_med.getText().toString() != "" && n_med.getText().length() > 0) {
nmed = Float.parseFloat(n_med.getText().toString());
} else {
nmed = 0;
}
return Float.toString( (((nmed - nsub) / (nmed + nsub))^2) *100 );
}
在另一个方程中,我将为另一个输入做,我也将实现 sqrt。类似的方程。 任何帮助都会很热心。 谢谢。
最佳答案
Java中的^
表示exclusive OR, XOR
操作,而不是升幂。您需要使用 Math.pow(x, 2) 或简单地将表达式与其本身相乘:
return Float.toString(100 * Math.pow( ((nmed - nsub) / (nmed + nsub)), 2));
或
float tmp = (nmed - nsub) / (nmed + nsub);
return Float.toString(100 * tmp*tmp);
关于java - 在方程中实现指数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19085715/