价值在方法的途中迷失了

时间:2012-05-10 14:57:40

标签: java android type-conversion

我还是一个初学者但是想要学习这样做可能做得非常愚蠢。我的问题是关于我想要制作的一个简单的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);
}

1 个答案:

答案 0 :(得分:5)

Java中的比较是用双等号“==”进行的。

仅仅通过将它们放在括号之间被认为是更好的形式来测试布尔值:

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