您好我需要帮助才能在Android中制作计算器我自己做了一点但是我正在寻找一种方法来添加2个数字,例如比答案更准确的5 + 5是10,而不是首先添加数字5而不是将其替换为另一个数字而不是添加它,是否有某种方法可以做到这一点,如何更改此部分:
public void onClick(View v) {
switch (v.getId()) {
case R.id.plus:
if (operand1 == 0) {
operand1 = Integer.parseInt(resultText.getText().toString());
resultText.setText("0");
operator = "+";
} else {
operand2 = Integer.parseInt(resultText.getText().toString());
answer = operand1 + operand2;
operand1 = answer;
resultText.setText("" + answer);
operand2 = 0;
answer = 0;
operator = "";
}
break;
使其成为5 + 5 = 10而不是5,而不是添加并用另一个数字替换它,而不是添加它。
这是我的完整代码:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
EditText resultText;
Button one1, two1, three1;
Button plus1;
Button four1;
Button five1;
Button six1;
Button minus1;
Button seven1;
Button eight1;
Button nine1;
Button multiply1;
Button clear;
Button zero1;
Button square1;
Button divide1, equals;
int operand1, operand2, answer;
String input, operator;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
resultText = (EditText) findViewById(R.id.space);
resultText.setText("0");
one1 = (Button) findViewById(R.id.one);
two1 = (Button) findViewById(R.id.two);
three1 = (Button) findViewById(R.id.three);
plus1 = (Button) findViewById(R.id.plus);
four1 = (Button) findViewById(R.id.four);
five1 = (Button) findViewById(R.id.five);
six1 = (Button) findViewById(R.id.six);
minus1 = (Button) findViewById(R.id.minus);
seven1 = (Button) findViewById(R.id.seven);
eight1 = (Button) findViewById(R.id.eight);
nine1 = (Button) findViewById(R.id.nine);
multiply1 = (Button) findViewById(R.id.multiply);
clear = (Button) findViewById(R.id.c);
zero1 = (Button) findViewById(R.id.zero);
square1 = (Button) findViewById(R.id.square);
divide1 = (Button) findViewById(R.id.divide);
equals = (Button) findViewById(R.id.equal);
one1.setOnClickListener(this);
two1.setOnClickListener(this);
three1.setOnClickListener(this);
plus1.setOnClickListener(this);
four1.setOnClickListener(this);
five1.setOnClickListener(this);
six1.setOnClickListener(this);
minus1.setOnClickListener(this);
seven1.setOnClickListener(this);
eight1.setOnClickListener(this);
nine1.setOnClickListener(this);
multiply1.setOnClickListener(this);
clear.setOnClickListener(this);
zero1.setOnClickListener(this);
square1.setOnClickListener(this);
divide1.setOnClickListener(this);
equals.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.plus:
if (operand1 == 0) {
operand1 = Integer.parseInt(resultText.getText().toString());
resultText.setText("0");
operator = "+";
} else {
operand2 = Integer.parseInt(resultText.getText().toString());
answer = operand1 + operand2;
operand1 = answer;
resultText.setText("" + answer);
operand2 = 0;
answer = 0;
operator = "";
}
break;
case R.id.minus:
if (operand1 == 0) {
operand1 = Integer.parseInt(resultText.getText().toString());
resultText.setText("0");
operator = "-";
} else {
operand2 = Integer.parseInt(resultText.getText().toString());
answer = operand1 - operand2;
operand1 = answer;
resultText.setText("" + answer);
operator = "";
operand2 = 0;
answer = 0;
}
break;
case R.id.multiply:
if (operand1 == 0) {
operand1 = Integer.parseInt(resultText.getText().toString());
operator = "*";
resultText.setText("0");
} else {
operand2 = Integer.parseInt(resultText.getText().toString());
answer = operand1 * operand2;
operand1 = answer;
resultText.setText("" + answer);
operand2 = 0;
answer = 0;
operator = "";
}
break;
case R.id.divide:
if (operand1 == 0) {
operand1 = Integer.parseInt(resultText.getText().toString());
operator = "/";
resultText.setText("0");
} else {
operand2 = Integer.parseInt(resultText.getText().toString());
answer = operand1 / operand2;
operand1 = answer;
resultText.setText("" + answer);
operand2 = 0;
answer = 0;
operator = "";
}
break;
case R.id.equal:
if (operand1 != 0) {
operand2 = Integer.parseInt(resultText.getText().toString());
switch (operator) {
case "+":
answer = operand1 + operand2;
break;
case "-":
answer = operand1 - operand2;
break;
case "*":
answer = operand1 * operand2;
break;
case "/":
answer = operand1 / operand2;
break;
default:
answer=0;
break;
}
operand1 = answer;
resultText.setText("" + answer);
operand2 = 0;
answer = 0;
}
break;
case R.id.square:
operand1 = Integer.parseInt(resultText.getText().toString());
answer = operand1 * operand1;
resultText.setText("" + answer);
operand1 = answer;
break;
case R.id.c:
operand1 = 0;
operand2 = 0;
answer = 0;
resultText.setText("0");
break;
default:
input = resultText.getText().toString();
if (input.equals("0"))
input = v.getTag().toString();
else
input += v.getTag().toString();
resultText.setText(input);
break;
}
}
}
试着看看你是否可以尽我所能帮助我。
答案 0 :(得分:0)
我不确定我是否理解您的确切要求,但看起来您在显示所需的字符串之前设置了操作数1来回答(" 5 + 5"。)
尝试将代码设置为以下内容:
public void onClick(View v) {
switch (v.getId()) {
case R.id.plus:
if (operand1 == 0) {
operand1 = Integer.parseInt(resultText.getText().toString());
resultText.setText("0");
operator = "+";
} else {
operand2 = Integer.parseInt(resultText.getText().toString());
answer = operand1 + operand2;
resultText.setText(operand1 + operator + operand2);
}
break;
然后为=创建一个单独的按钮,它将触发以下内容:
resultText.setText(answer);
answer = 0;
operand1 = 0;
operand2 = 0;
operator = "";
这看起来仍然很笨拙,但是如果没有看到更多的代码,我就无法确定它是否符合您的构建与否。基本上,显示完整的字符串(" 5 + 5"),然后按Enter键显示答案(" 10")并清理所有其他变量。如果您不想通过单独的按钮触发答案,则可能会将其设置为延迟。