2012-05-10 3 views
0

私はまだ初心者ですが、おそらく何か非常にばかげたことを学ぶことを試みています。私の質問は、私が作ろうとしている簡単なAndroidの計算機についてですが、私は理解していると思っていた基本的なJavaに関する問題だと思います。私は押されたボタンから値を取得し、それをメソッドに渡して入力を表示し、入力のグローバル変数を設定します。 "1"を押すとメソッドヘッダーの値は "1"になりますが、そのメソッドでは失われます。たぶん、私は思ったように、Javaの型をどのように扱うか分からないかもしれません。私は一度に論理が働いていたが、それがどのように表示されたかを変えなければならず、もちろん論理をどこかで揺さぶった。とにかく、合格陳述と受取方法を今投稿します。前もって感謝します。方法への道のりで価値が失われる

public void initButtons() 
{ 
    //register buttons as listeners 
    final Button button1 = (Button) findViewById(R.id.button1); 
    final Button button2 = (Button) findViewById(R.id.button2); 
    final Button button3 = (Button) findViewById(R.id.button3); 
    final Button button4 = (Button) findViewById(R.id.button4); 
    final Button button5 = (Button) findViewById(R.id.button5); 
    final Button button6 = (Button) findViewById(R.id.button6); 
    final Button button7 = (Button) findViewById(R.id.button7); 
    final Button button8 = (Button) findViewById(R.id.button8); 
    final Button button9 = (Button) findViewById(R.id.button9); 
    final Button button10 = (Button) findViewById(R.id.button10); 
    final Button addButton = (Button) findViewById(R.id.addButton); 
    final Button subButton = (Button) findViewById(R.id.subButton); 
    final Button multButton = (Button) findViewById(R.id.multButton); 
    final Button divButton = (Button) findViewById(R.id.divButton); 
    final Button calcButton = (Button) findViewById(R.id.calcButton); 
    final Button clearButton = (Button) findViewById(R.id.clearButton); 

    //when button is pressed, send num to calc function 
    button1.setOnClickListener 
    (new Button.OnClickListener() 
     { 
      public void onClick(View v) 
      { 
       inputString = button1.getText().toString(); 
       displayCalc(inputString); 
       //displayCalc(1.0); 
      } 
     } 
    ); 

    private void displayCalc(String curValue) 
{ 
    if (hasChanged = false) 
    { 
     //display number if reset 
     String display = curValue; 
     number1 = Double.parseDouble(display); 
    } 
    else              
    { 
    // display = display + operator + curValue; 
     //number2 = Double.parseDouble(curValue); 
    } 
    //showDisplay(display); 
} 

答えて

5

Javaの比較は、二重等号 "=="で行われます。それは、単にこのような括弧の間にそれらを置くことによってブール値をテストするために、より良い形と考えられている

:あなたの応答のための

if(myBool){ 
    //do something 
} 
else if(!myOtherBool){ 
    //do something else 
} 
+0

おかげで、私はそれを変更しました。それはもともとの方法ですが、何らかの理由で変更しました。値はまだ変換されていません。私がデバッグすると、 '1'ボタンを押すと変数 'number1'の値は0.0になります。したがって、curValue = "1"なのでdisplay変数で失われているはずですが、どうすればよいか分かりません。助言がありますか? – codeMagic

+0

hasChanged変数はクラス定義の直後に宣言されています – codeMagic

+0

申し訳ありません。curValueが解析する前にcurValueが "1"に等しい場合、問題がどのようになるかわかりません。 デバッグ中に解析操作後にnumber1の内容を確認していますか? – OlivierLi

関連する問題