2014-02-05 171 views
-3
EditText txtDisplay = (EditText) findViewById(R.id.txtDisplay); 
    display = txtDisplay.getText().toString(); 
    operator = display.split("\\d|\\."); 
    operand = display.split("\\+|\\-|\\*|\\/"); 

    if(operator[0] == "+"){ 
     answer = Double.valueOf(operand[0]) + Double.valueOf(operand[1]); 
    } 

    if(operator[0] == "-"){ 
     answer = Double.valueOf(operand[0]) - Double.valueOf(operand[1]); 
    } 

    if(operator[0] == "*"){ 
     answer = Double.valueOf(operand[0]) * Double.valueOf(operand[1]); 
    } 

    if(operator[0] == "/"){ 
     answer = Double.valueOf(operand[0])/Double.valueOf(operand[1]); 
    } 

    txtDisplay.setText(String.valueOf(answer)); 

顯示的答案總是0.0,我不知道我做錯了什麼。它是我對數據類型的轉換嗎?我想用我的兩個輸入做一個數學運算

+0

您是否調試過代碼?我想沒有輸入'if'。 – user1983983

+0

你應該'Log.i'運算符和'operand'的內容全部在 –

+0

之前使用sqlite直接在一行中得到結果。只需傳遞用edittext編寫的表達式即可。並得到結果 –

回答

2

您必須使用equals()方法來比較字符串標識。不是==運營商。

if(operator[0].equals("+")) { 
    answer = Double.valueOf(operand[0]) + Double.valueOf(operand[1]); 
} 
相關問題