2012-05-10 38 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」,但我不知道如何。有什麼建議麼? – codeMagic

+0

hasChanged變量在類定義後立即聲明 – codeMagic

+0

對不起,如果curValue在解析之前等於「1」,我真的不知道問題是什麼。 您確定在調試時正在查看解析操作後的number1內容嗎? – OlivierLi