2012-01-17 65 views
0

我做了一個擺動計算器作爲我做的功課的一部分,並且特別希望繼續減法的一些建議。隨着繼續加起來,它足夠直接了(i.o.w。添加操作數,然後從那裏繼續添加)。這就是我所做的。關於繼續減法

if(totalCount > 1) 
    { 
     if(sbOne.length() > 0) 
     { 
      operandOne = Double.parseDouble(sbOne.toString()); 
     } 
     else 
     { 
      operandOne = 0; 
     } 

     if(sbTwo.length() > 0) 
     { 
      operandTwo = Double.parseDouble(sbTwo.toString()); 

     } 
     else 
     { 
      operandTwo = 0; 
     } 
     result = operandOne + operandTwo; 
     totalResult += result; 
     screenResult = Double.toString(totalResult); 
     txtDisplay.setText(screenResult); 
     notCalculate = true; 
     sbOne.setLength(0); 
     sbTwo.setLength(0); 

我如何能實現從另一箇中減去一個操作數,然後繼續從那裏起減去相同的結果。

+3

你的意思是像'totalResult - =結果;'? – 2012-01-17 19:43:34

+0

不完全確定你在這裏問什麼..短的^ – Alex 2012-01-17 19:46:59

+0

相同的評論亞歷克斯 – 2012-01-17 20:05:32

回答

2

你的代碼似乎很混亂,特別是因爲它不完整且不可編譯。我對你的代碼的解釋如下:你有兩個可能的正值,你加在一起,因此你把這個總和加到你已經有的總和上。我對你的問題的解釋如下:你想從總和中減去這個總和。解決方案就像Hot Licks所說的那樣,只是使用以下操作:totalResult -= result;。如果你想有可能決定你是否要添加或減去,加一個布爾標誌,即:

/*somewhere in your code to determine whether you add or subtract, 
    have a button or something which changes this value. 
*/ 
boolean isAdding = true; 

//... 

//button pressed 
isAdding = false; 

//... 

//your calculating code goes here 
if(isAdding) 
    totalResult += result; 
else 
    totalResult -= result; 

//all of the other stuff 
+0

感謝您的建議。 – Arianule 2012-01-24 06:56:50