Здравствуйте, мне нужна помощь в создании калькулятора на Android, я сделал немного сам, но я ищу способ, которым вы добавляете 2 числа, например. 5 + 5, чем ответ приходит как 10, а не сначала путем добавления номера 5, чем замена его другим номером, и чем он добавляется, есть ли определенный способ сделать это, заранее заранее, например. Как я могу изменить эту часть:Нужна помощь в создании калькулятора в Android
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;
}
}
}
Try, чтобы увидеть, если и может помочь мне столько, сколько вы можете.
Я не уверен, что вы имеете в виду, вам нужно одно текстовое поле редактирования, в которое вы вводите строку «5 + 5», а затем калькулятор напечатает 10 результатов? Потому что введенный вами код почти имитирует классический калькулятор, связывая операции с предыдущим результатом. – Ped7g
Вы хотите показать операнды на экране перед их добавлением? – DCruz22
yes thats, что я имею в виду, я хочу добавить 2 числа, например 5 + 5 = 10, я не хочу заменять число и, чем получить результат, правильно, например, если я пишу 5, нажмите +, чтобы добавить другой номер, номер заменяется и после того, как он равен 10, я не хочу, чтобы это просто, как 5 + 5, чем когда я нажимаю, равно 10, я хочу, чтобы 5 + 5 отображалось в текстовом поле – kenshin92