cod*_*gic 0 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);
}
Run Code Online (Sandbox Code Playgroud)
Java中的比较用双等号"=="进行.
它被认为是更好的形式来测试布尔值只需将它们放在括号之间,如下所示:
if(myBool){
//do something
}
else if(!myOtherBool){
//do something else
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
112 次 |
| 最近记录: |