在 Java 中显示 Android 计算器的最后一个运算符
Display the last operator for an Android calculator in Java
我正在为 Java 中的 Android 编写一个计算器,在我的程序完成之前还有最后一件事要做。
比如我按这个顺序:5,+,-,/,*,5,运算结果就是“5*5”。实际上,当我这样做时,它只保留第一个操作员。我该如何解决?我无法完成它,它让我发疯。如果有人能帮助我,我将不胜感激。
这是我的代码,我希望它很干净并且你能理解它:
package com.example.calcapp_training;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
Button btnMinus, btnDiv, btnMult, btnPlus, btnDel, btnEqual, btnPoint, btn0, btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9;
TextView input, result;
TextView error;
ImageView image;
double mValueOne, mValueTwo;
int Calculation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView errorMessage = findViewById(R.id.error);
final TextView welcomeMessage = findViewById(R.id.welcome);
btn0 = (Button) findViewById(R.id.btn0);
btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn2);
btn3 = (Button) findViewById(R.id.btn3);
btn4 = (Button) findViewById(R.id.btn4);
btn5 = (Button) findViewById(R.id.btn5);
btn6 = (Button) findViewById(R.id.btn6);
btn7 = (Button) findViewById(R.id.btn7);
btn8 = (Button) findViewById(R.id.btn8);
btn9 = (Button) findViewById(R.id.btn9);
btnPlus = (Button) findViewById(R.id.btnPlus );
btnMinus = (Button) findViewById(R.id.btnMinus );
btnMult = (Button) findViewById(R.id.btnMult );
btnDiv = (Button) findViewById(R.id.btnDiv );
btnDel = (Button) findViewById(R.id.btnDel);
btnEqual = (Button) findViewById(R.id.btnEqual);
btnPoint = (Button) findViewById(R.id.btnPoint);
input = (TextView) findViewById(R.id.input);
result = (TextView) findViewById(R.id.result);
error = (TextView) findViewById(R.id.error);
image = (ImageView) findViewById(R.id.image);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
welcomeMessage.setVisibility(View.INVISIBLE);
result.setText(result.getText() + "1");
}
});
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
welcomeMessage.setVisibility(View.INVISIBLE);
result.setText(result.getText() + "2");
}
});
btn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
welcomeMessage.setVisibility(View.INVISIBLE);
result.setText(result.getText() + "3");
}
});
btn4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
welcomeMessage.setVisibility(View.INVISIBLE);
result.setText(result.getText() + "4");
}
});
btn5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
welcomeMessage.setVisibility(View.INVISIBLE);
result.setText(result.getText() + "5");
}
});
btn6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
welcomeMessage.setVisibility(View.INVISIBLE);
result.setText(result.getText() + "6");
}
});
btn7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
welcomeMessage.setVisibility(View.INVISIBLE);
result.setText(result.getText() + "7");
}
});
btn8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
welcomeMessage.setVisibility(View.INVISIBLE);
result.setText(result.getText() + "8");
}
});
btn9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
welcomeMessage.setVisibility(View.INVISIBLE);
result.setText(result.getText() + "9");
}
});
btn0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
welcomeMessage.setVisibility(View.INVISIBLE);
result.setText(result.getText() + "0");
}
});
btnDel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
welcomeMessage.setVisibility(View.INVISIBLE);
result.setText("");
input.setText("");
}
});
btnPoint.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
welcomeMessage.setVisibility(View.INVISIBLE);
result.setText(result.getText() + ".");
}
});
btnPlus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if( result.getText().toString().equals("") != true ) {
mValueOne=Double.parseDouble(result.getText()+"");
Calculation = 1;
result.setText(null);
}
}
});
btnMinus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if( result.getText().toString().equals("") != true ) {
mValueOne=Double.parseDouble(result.getText()+"");
Calculation = 2;
result.setText(null);
}
}
});
btnMult.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if( result.getText().toString().equals("") != true ) {
mValueOne=Double.parseDouble(result.getText()+"");
Calculation = 3;
result.setText(null);
}
}
});
btnDiv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if( result.getText().toString().equals("") != true ) {
mValueOne=Double.parseDouble(result.getText()+"");
Calculation = 4;
result.setText(null);
}
}
});
btnEqual.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mValueTwo = Double.parseDouble(result.getText() + "");
if (Calculation == 1) {
result.setText(mValueOne + mValueTwo + "");
Calculation = 1;
}
else if (Calculation == 2) {
result.setText(mValueOne - mValueTwo + "");
Calculation = 2;
}
else if (Calculation == 3) {
result.setText(mValueOne * mValueTwo + "");
Calculation = 3;
}
else if (Calculation == 4) {
double a = Math.round(mValueOne / mValueTwo);
result.setText(a + "" );
if(mValueTwo == 0) {
errorMessage.setVisibility(View.VISIBLE);
result.setText(mValueOne / mValueTwo + "");
Calculation = 4;
} else {
errorMessage.setVisibility(View.INVISIBLE);
}
Calculation = 4;
}
}
});
}
}
把所有的操作按钮都点击Calculation
这样的外部条件:
btnPlus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if( result.getText().toString().equals("") != true ) {
mValueOne=Double.parseDouble(result.getText()+"");
result.setText(null);
}
Calculation = 1;
}
});
我正在为 Java 中的 Android 编写一个计算器,在我的程序完成之前还有最后一件事要做。
比如我按这个顺序:5,+,-,/,*,5,运算结果就是“5*5”。实际上,当我这样做时,它只保留第一个操作员。我该如何解决?我无法完成它,它让我发疯。如果有人能帮助我,我将不胜感激。
这是我的代码,我希望它很干净并且你能理解它:
package com.example.calcapp_training;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
Button btnMinus, btnDiv, btnMult, btnPlus, btnDel, btnEqual, btnPoint, btn0, btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9;
TextView input, result;
TextView error;
ImageView image;
double mValueOne, mValueTwo;
int Calculation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView errorMessage = findViewById(R.id.error);
final TextView welcomeMessage = findViewById(R.id.welcome);
btn0 = (Button) findViewById(R.id.btn0);
btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn2);
btn3 = (Button) findViewById(R.id.btn3);
btn4 = (Button) findViewById(R.id.btn4);
btn5 = (Button) findViewById(R.id.btn5);
btn6 = (Button) findViewById(R.id.btn6);
btn7 = (Button) findViewById(R.id.btn7);
btn8 = (Button) findViewById(R.id.btn8);
btn9 = (Button) findViewById(R.id.btn9);
btnPlus = (Button) findViewById(R.id.btnPlus );
btnMinus = (Button) findViewById(R.id.btnMinus );
btnMult = (Button) findViewById(R.id.btnMult );
btnDiv = (Button) findViewById(R.id.btnDiv );
btnDel = (Button) findViewById(R.id.btnDel);
btnEqual = (Button) findViewById(R.id.btnEqual);
btnPoint = (Button) findViewById(R.id.btnPoint);
input = (TextView) findViewById(R.id.input);
result = (TextView) findViewById(R.id.result);
error = (TextView) findViewById(R.id.error);
image = (ImageView) findViewById(R.id.image);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
welcomeMessage.setVisibility(View.INVISIBLE);
result.setText(result.getText() + "1");
}
});
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
welcomeMessage.setVisibility(View.INVISIBLE);
result.setText(result.getText() + "2");
}
});
btn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
welcomeMessage.setVisibility(View.INVISIBLE);
result.setText(result.getText() + "3");
}
});
btn4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
welcomeMessage.setVisibility(View.INVISIBLE);
result.setText(result.getText() + "4");
}
});
btn5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
welcomeMessage.setVisibility(View.INVISIBLE);
result.setText(result.getText() + "5");
}
});
btn6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
welcomeMessage.setVisibility(View.INVISIBLE);
result.setText(result.getText() + "6");
}
});
btn7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
welcomeMessage.setVisibility(View.INVISIBLE);
result.setText(result.getText() + "7");
}
});
btn8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
welcomeMessage.setVisibility(View.INVISIBLE);
result.setText(result.getText() + "8");
}
});
btn9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
welcomeMessage.setVisibility(View.INVISIBLE);
result.setText(result.getText() + "9");
}
});
btn0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
welcomeMessage.setVisibility(View.INVISIBLE);
result.setText(result.getText() + "0");
}
});
btnDel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
welcomeMessage.setVisibility(View.INVISIBLE);
result.setText("");
input.setText("");
}
});
btnPoint.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
welcomeMessage.setVisibility(View.INVISIBLE);
result.setText(result.getText() + ".");
}
});
btnPlus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if( result.getText().toString().equals("") != true ) {
mValueOne=Double.parseDouble(result.getText()+"");
Calculation = 1;
result.setText(null);
}
}
});
btnMinus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if( result.getText().toString().equals("") != true ) {
mValueOne=Double.parseDouble(result.getText()+"");
Calculation = 2;
result.setText(null);
}
}
});
btnMult.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if( result.getText().toString().equals("") != true ) {
mValueOne=Double.parseDouble(result.getText()+"");
Calculation = 3;
result.setText(null);
}
}
});
btnDiv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if( result.getText().toString().equals("") != true ) {
mValueOne=Double.parseDouble(result.getText()+"");
Calculation = 4;
result.setText(null);
}
}
});
btnEqual.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mValueTwo = Double.parseDouble(result.getText() + "");
if (Calculation == 1) {
result.setText(mValueOne + mValueTwo + "");
Calculation = 1;
}
else if (Calculation == 2) {
result.setText(mValueOne - mValueTwo + "");
Calculation = 2;
}
else if (Calculation == 3) {
result.setText(mValueOne * mValueTwo + "");
Calculation = 3;
}
else if (Calculation == 4) {
double a = Math.round(mValueOne / mValueTwo);
result.setText(a + "" );
if(mValueTwo == 0) {
errorMessage.setVisibility(View.VISIBLE);
result.setText(mValueOne / mValueTwo + "");
Calculation = 4;
} else {
errorMessage.setVisibility(View.INVISIBLE);
}
Calculation = 4;
}
}
});
}
}
把所有的操作按钮都点击Calculation
这样的外部条件:
btnPlus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if( result.getText().toString().equals("") != true ) {
mValueOne=Double.parseDouble(result.getText()+"");
result.setText(null);
}
Calculation = 1;
}
});