2012-02-28 7 views
0

まずはEclipseを使用してください。私はAndroidの電卓を作っています。問題があることを除いて、私はすべてのコードを持っています。私の変数の1つが使用されていません。私のtotal1変数は基本的にプログラムのカウンタです。 2つの数値を加算して等号を押すと、常に「0.0」が出力され、これがtotal1の値になります。それは何らかの理由で、プログラム全体を通して決して変化しなかったことを意味します。私はprintステートメントを使ってこれをテストしました。私はtotal1の値を変更し、割り当てた値を出力します。何が問題なの?変数はAndroid電卓では使用されません

public class HelloAndroidActivity extends Activity { 
    public EditText display; 
    String display1; 
    double displayValue; 

    // Program is printing out total1, as if it weren't maninpulated in the code 
    double total1 = 0.0; 
    double total2 = 0.0; 

    char theOperator; 
    public String buttonText; 
    public Button ButtonAdd, ButtonEqual, ButtonMultiply, ButtonDivide, ButtonMinus; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     display = (EditText) findViewById(R.id.editText1); 

     // Could it have something to do with this if statement? 
     if (display.length() != 0) { 
      display1 = display.getText().toString(); 
      displayValue = Double.parseDouble(display1); 
     } 
    } 

    //Get the operator of the multiply, divide, subtract, add buttons when they are clicked and then do the following 
    public void getOperator(String btnText) { 
     theOperator = btnText.charAt(0); 

     total1 += displayValue; 
     display.setText(""); 
    } 

    // Display a string in the box when a number button is clicked 
    public void onClick(View v) { 
     switch (v.getId()) { 
     case R.id.bOne: 
      display.append("1");   
      break; 
     case R.id.bTwo: 
      display.append("2"); 
      break; 
     case R.id.bThree: 
      display.append("3"); 
      break; 
     case R.id.bFour: 
      display.append("4"); 
      break; 
     case R.id.bFive: 
      display.append("5"); 
      break; 
     case R.id.bSix: 
      display.append("6"); 
      break; 
     case R.id.bSeven: 
      display.append("7"); 
      break; 
     case R.id.bEight: 
      display.append("8"); 
      break; 
     case R.id.bNine: 
      display.append("9"); 
      break; 
     case R.id.bZero: 
      display.append("0"); 
      break; 
     case R.id.bPoint: 
      display.append("."); 
      break; 
     case R.id.bClear: 
      total2 = 0.0; 
      display.setText(""); 
      break; 
     case R.id.bAdd: 
      buttonText = "+"; 
      ButtonAdd = (Button) findViewById(R.id.bAdd); 
      ButtonAdd.setText(buttonText); 
      getOperator(buttonText); 
      break; 
     case R.id.bMinus: 
      buttonText = "-"; 
      ButtonMinus = (Button) findViewById(R.id.bMinus); 
      ButtonMinus.setText(buttonText); 
      getOperator(buttonText); 
      break; 
     case R.id.bMultiply: 
      buttonText = "*"; 
      ButtonMultiply = (Button) findViewById(R.id.bMultiply); 
      ButtonMultiply.setText(buttonText); 
      getOperator(buttonText); 
      break; 
     case R.id.bDivide: 
      buttonText = "/"; 
      ButtonDivide = (Button) findViewById(R.id.bDivide); 
      ButtonDivide.setText(buttonText); 
      getOperator(buttonText); 
      break; 

     // Here's the equals button. When I click it it prints out "0.0", the value of total1, instead of adding the total below 
     case R.id.bEqual: 
      switch (theOperator) { 
      case '+': 
       total2 = total1 + displayValue; 
       break; 
      case '-': 
       total2 = total1 - displayValue; 
       break; 
      case '*': 
       total2 = total1 * displayValue; 
       break; 
      case '/': 
       total2 = total1/displayValue; 
       break; 
      } 
      display.setText(Double.toString(total2)); 
      total1 = 0.0; 
      break; 
     }      
    } 
} 
+0

あなたはTOTAL1を設定しています= 0.0;同じで – JProgrammer

+0

いいえ、そうではありません。それをコメントアウトしても印刷されています0.0 – recheej

答えて

1

get演算子メソッドにdisplayValueを追加しようとしていますが、この値は変更していません。あなたは現在の表示値を保存する必要がある場合

public void getOperator(String btnText){ 
    theOperator = btnText.charAt(0); 



    total1+=Double.parseDouble(display.getText().ToString()); 
    display.setText(""); 
} 

またはこのような:あなたはそれがこのようにする必要がありtotal1に追加する前に、ディスプレイからこの値をペアリングする必要があります

public void getOperator(String btnText){ 
    theOperator = btnText.charAt(0); 


    displayValue = Double.parseDouble(display.getText().ToString()); 
    total1+=displayValue; 
    display.setText(""); 
} 
+0

if文で既に行われていませんか? – recheej

+0

ifステートメントはonCreateメソッド内にのみあります。このメソッドは、ディスプレイの最初の作成時に一度だけ呼び出されます。毎回更新する必要があります。 – jzworkman

+0

ありがとう、私のアプリは今働いています。 – recheej

1

私はあなたのtotal1変数が変更され、そこにそれがdisplayValueの値がそれに追加しただけの場所を参照してください。 displayValueは、editText1コントロールのコンテンツ(ソースがない)からロードされたonCreateメソッド(アクティビティライフサイクルごとに1回だけ呼び出されます)で1回のみ変更されます。

だから私は短く私は驚くことではありませんtotal1は変更されていません。これは、アプリのロジック全体の流れをチェックすることです。

関連する問題