我是新来的(准确地说是 5 天前),用 Android 和 Java 制作基本程序。这是我正在处理的代码,但我陷入了此代码的 'else if' on sendAmount() 部分。
我收到一个错误,提示“对于参数类型 double, double ,运算符 ^ 未定义”。
现在我的问题是,如何让它工作并显示结果?
private EditText entPrinAmt;
private EditText entIntRate;
private EditText entTol;
private EditText entMonAmt;
private TextView txtFactor;
private TextView txtFutValue;
private TextView txtIntIncome;
private TextView txtEffRate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
entPrinAmt = (EditText) findViewById(R.id.ent_prin_amt);
entIntRate = (EditText) findViewById(R.id.ent_int_rate);
entTol = (EditText) findViewById(R.id.ent_tol);
entMonAmt = (EditText) findViewById(R.id.ent_mon_amt);
txtFactor = (TextView) findViewById(R.id.txtFactor);
txtFutValue = (TextView) findViewById(R.id.txtFutureVal);
txtIntIncome = (TextView) findViewById(R.id.txtIntIncome);
txtEffRate = (TextView) findViewById(R.id.txtEffRate);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void sendAmount(View view) {
DecimalFormat df = new DecimalFormat("#,###,###,###,###,###.##");
DecimalFormat factorFormat = new DecimalFormat("#,###.#####################");
double dPA = Double.parseDouble(entPrinAmt.getText().toString());
double dIR = Double.parseDouble(entIntRate.getText().toString());
double dTL = Double.parseDouble(entTol.getText().toString());
double dMA = Double.parseDouble(entMonAmt.getText().toString());
if (dPA < 0){
Context context = getApplicationContext();
CharSequence txtToast = "Must not be negative!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, txtToast, duration);
toast.show();
}
else if (dPA == 0 && dIR>0 && dTL>0 && dMA>0){
Double resultPA;
resultPA = ((dMA * ((1+dIR/12)^dTL-1)/((1+dIR/12)^dTL*dIR/12)),2);
/*String f = df.format(resultPA); <-v-DO NOT MIND THIS
txtFactor.setText("Principal Amount: P" + f);*/
}
else {
Context context = getApplicationContext();
CharSequence txtToast = "Can't Compute Principal Amount!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, txtToast, duration);
toast.show();
}
}
顺便说一句,我从这里得到了这个方程:
return ROUNDOFF((nPmt*((1+nRate/12)^nPer-1) / ((1+nRate/12)^nPer*nRate/12)),2)
我认为这是来自 C 或 C++ 平台。我不明白“,2”部分。我如何将其应用到我正在学习的类(class)中?任何帮助将不胜感激。
最佳答案
^
运算符是按位异或,而不是幂。事实上,Java 没有定义幂运算符。您应该使用 Math.pow
相反。
此外,您应该使用 BigDecimal
当使用货币时,出于多种原因,您会在此处找到解释。
关于java - double 和 '^' 运算符,不可能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22125600/