gpt4 book ai didi

android - 具有多种操作的简单计算器 - Android

转载 作者:搜寻专家 更新时间:2023-11-01 08:51:40 25 4
gpt4 key购买 nike

我创建了一个简单的计算器来计算两个输入(例如 2 + 2 = 4)。我现在想做的是让应用程序计算多个操作,例如(例如 2 + 2 * 4 - 1 = 15)。有人可以帮我处理我的代码吗?这是我的代码。

public class MainActivity extends Activity {

public String str ="";

Character op = 'q';
int i,num,numtemp;
EditText showResult;
String displayStr = "";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

showResult = (EditText)findViewById(R.id.result_id);


}
public void btn1Clicked(View v){
insert(1);

}

public void btn2Clicked(View v){
insert(2);

}
public void btn3Clicked(View v){
insert(3);

}
public void btn4Clicked(View v){
insert(4);

}
public void btn5Clicked(View v){
insert(5);

}
public void btn6Clicked(View v){
insert(6);
}
public void btn7Clicked(View v){
insert(7);

}
public void btn8Clicked(View v){
insert(8);

}
public void btn9Clicked(View v){
insert(9);

}

public void btn0Clicked(View v){
insert(0);

}


public void btnplusClicked(View v){
perform();
op = '+';
displayStr += "+";
}

public void btnminusClicked(View v){
perform();
op = '-';
displayStr += "-";

}
public void btndivideClicked(View v){
perform();
op = '/';
displayStr += "/";
}
public void btnmultiClicked(View v){
perform();
op = '*';
displayStr += "*";
}
public void btnequalClicked(View v){
calculate();

}

public void btnclearClicked(View v){
reset();
}
private void reset() {
// TODO Auto-generated method stub
str ="";
op ='q';
num = 0;
numtemp = 0;
displayStr = "";
showResult.setText("");
}
private void insert(int j) {
// TODO Auto-generated method stub
str = str+Integer.toString(j);
num = Integer.valueOf(str).intValue();

displayStr += Integer.toString(j);
showResult.setText(displayStr);

}
private void perform() {
// TODO Auto-generated method stub
str = "";
numtemp = num;
}
private void calculate() {
// TODO Auto-generated method stub
String first=String.valueOf(numtemp);
String second=String.valueOf(num);

if(op == '+')
num = numtemp+num;
else if(op == '-')
num = numtemp-num;
else if(op == '/')
num = numtemp/num;
else if(op == '*')
num = numtemp*num;
showResult.setText(first+op+second+"="+num);
}

}

最佳答案

关于android - 具有多种操作的简单计算器 - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22704849/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com