2014-03-02 39 views
0

我是新來的人(確切的說是5天)在Android和Java中製作基本程序。以下是我正在處理的代碼,並且我堅持在'else if'on sendAmount()此代碼的一部分。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(); 
    } 
} 

BTW,我從這裏公式:

回報舍人((nPmt *((1 + nRate/12)^ NPER-1)/((1 + nRate/12)^nPer * nRate/12)),2)

我認爲這是來自C或C++平臺。我不明白',2'部分。我怎樣才能將它應用到我正在學習的課程中?任何幫助將不勝感激。

回答

6

^操作是按位異或,不是權力。事實上,Java沒有定義權力運營商。您應該改用Math.pow

此外,在使用貨幣時,您應該使用BigDecimal,有幾個原因可以在SO上找到。

+0

我真的認爲它的力量..香港專業教育學院今天學到了一些東西,謝謝。 – Daichi

0

^是按位異或運算,很可能要

double d = Math.pow(a, b); // a^^b 
0

使用pow功能。

double output = Math.pow(x,y); 
1

是,在Java 操作^(按位XOR)是未定義的參數類型(多個)雙,雙。 使用Math.pow(double , double)而不是^

所以,你的代碼看起來就像這樣:

resultPA = ((dMA * (Math.pow((1+dIR/12),dTL-1))/(Math.pow((1+dIR/12),dTL)*dIR/12)),2); 
+0

哦,謝謝你!但現在我得到了一個錯誤。說:「作業的左側必須是一個變量」 – Daichi

+0

不客氣,但你已經選擇了你的答案!請提出一個新問題,不要發表評論。 – mok

相關問題