2012-02-28 2 views
1

저는 Android Calculator를 프로그래밍하고 있습니다. 나는 모든 코드를 가지고 있지만, 계산기를 사용할 때 항상 "0.0"을 프린트하는데, 이것은 total1과 total2에 대해 설정 한 원래 값입니다. 그래서, 제 가정은 그 두 가지 변수가 메소드에 나타나지 않는다는 것입니다. 그래서 나는 나머지 코드를 사용하여 변경할 수 있도록 정적 변수로 설정해야한다고 가정합니다. 고정 변수를 잘못 설정했을 수 있습니다. 하지만 total1과 total2를 정적 변수로 설정 한 후에도 동일한 문제가 발생합니다. 내 프로그램이 작동하지 않는 이유에 대한 내 추측은 내 displayValue 변수입니다. 내 if 문에서 지역 변수 displayValue가 사용되지 않는다고 말합니다. 나는 어떤 도움을 주셔서 감사합니다.안드로이드 계산기 코드의 정적 변수에 문제가 있습니까?

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의 값을 ZERO로 지정하는 displayValue 인스턴스 변수에 값을 할당하지 않고 total1 변수에 total1을 할당합니다. & total2 변수.

관련 문제