2012-02-28 9 views
1

私はAndroid Calculatorをプログラミングしています。私はすべてのコードを持っていますが、電卓を使用すると、常に "0.0"が出力されます。これはtotal1とtotal2に設定されたオリジナルです。だから、私の前提は、メソッドはそれらの2つの変数を見ていないということです。だから私は静的変数としてそれらのコードの残りの部分を使用し、それらを変更できるようにそれらを設定する必要が仮定します。静的変数を間違って設定している可能性があります。しかし、total1とtotal2を静的変数に設定した後も、私は同じ問題を抱えています。なぜ私のプログラムが動作していないのかについての私の他の推測は、私のdisplayValue変数です。私のifステートメントでは、ローカル変数displayValueは使用されていないと言います。助けていただければ幸いです。Android Calculatorコードの静的変数に関する問題?

package rechee.cool; 

import android.app.Activity; 

import android.os.Bundle; 
import android.text.Editable; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 




public class HelloAndroidActivity extends Activity { 


/** Called when the activity is first created. */ 
public EditText display; 
    String display1; 
double displayValue; 

// I want to be able to use the following two variables everywhere in the code. 
static double total1=0.0; 
static 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); 




    if(display.length()!=0){ 
    String display1= display.getText().toString(); 
    // Says local variable displayValue has not been used. May be my problem? 
    double displayValue= Double.parseDouble(display1); 
} 
} 








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



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







     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; 
       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

なぜonCreate()メソッドでdisplayValue変数を再宣言していますか? – JProgrammer

答えて

0

変数ローカルの変数は、onCreateの範囲にのみ再宣言しています。

display1 = display.getText().toString(); 
// v^these now refer to the members of class HelloAndroidActivity 
displayValue = Double.parseDouble(display1); 

+0

displayValueを静的変数にする必要がありますか?または、プログラム全体のスコープに対してdisplayValueを作成することはできませんか? display1はOnCreateにのみローカルではありませんか?それは...ですか? – recheej

+0

@recheejはいそうです。クラスメンバを参照する場合は、型を変更する必要はありません。クラスのすべてのインスタンス間で変数を共有する場合は、単に 'static'を使用してください。アップデート – paislee

+0

を参照してくださいああ私は理解しています。私は変数の型を変数の前に置いていたので、新しい変数として割り当てられました。わかりました。しかし何らかの理由で私はまだ0.0が印刷されています。私のコードのどこに問題があるのですか? – recheej

0

あなたのコードはそれほど明確ではない読んで推奨してみてください。同じクラスで使用するには静的であると定義する必要はありません。あなたのコードの本当の原因はdisplayValueです。 displayValueの値をゼロにしたdisplayValueインスタンス変数には決して値を割り当てず、total2変数にtotal1 &を割り当てます。

関連する問題