android studio实现计算器的加减乘除、乘方、开方
时间: 2023-12-21 11:04:03 浏览: 129
android studio实现简单的计算器(无bug)
首先,在 Android Studio 中创建一个新项目,然后在布局文件中添加一个 TextView 和多个 Button,以实现计算器的界面。
接下来,在 MainActivity.java 中编写代码来实现计算器的功能。以下是一个简单的实现示例:
```java
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.text.DecimalFormat;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private TextView mDisplayText;
private Button mButton0, mButton1, mButton2, mButton3, mButton4, mButton5, mButton6, mButton7, mButton8, mButton9;
private Button mButtonPlus, mButtonMinus, mButtonMultiply, mButtonDivide, mButtonPower, mButtonSqrt, mButtonEqual, mButtonClear;
private String mNumber1 = "", mNumber2 = "", mOperator = "";
private DecimalFormat mDecimalFormat;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDisplayText = findViewById(R.id.display_text);
mButton0 = findViewById(R.id.button_0);
mButton1 = findViewById(R.id.button_1);
mButton2 = findViewById(R.id.button_2);
mButton3 = findViewById(R.id.button_3);
mButton4 = findViewById(R.id.button_4);
mButton5 = findViewById(R.id.button_5);
mButton6 = findViewById(R.id.button_6);
mButton7 = findViewById(R.id.button_7);
mButton8 = findViewById(R.id.button_8);
mButton9 = findViewById(R.id.button_9);
mButtonPlus = findViewById(R.id.button_plus);
mButtonMinus = findViewById(R.id.button_minus);
mButtonMultiply = findViewById(R.id.button_multiply);
mButtonDivide = findViewById(R.id.button_divide);
mButtonPower = findViewById(R.id.button_power);
mButtonSqrt = findViewById(R.id.button_sqrt);
mButtonEqual = findViewById(R.id.button_equal);
mButtonClear = findViewById(R.id.button_clear);
mDecimalFormat = new DecimalFormat("#.##########");
mButton0.setOnClickListener(this);
mButton1.setOnClickListener(this);
mButton2.setOnClickListener(this);
mButton3.setOnClickListener(this);
mButton4.setOnClickListener(this);
mButton5.setOnClickListener(this);
mButton6.setOnClickListener(this);
mButton7.setOnClickListener(this);
mButton8.setOnClickListener(this);
mButton9.setOnClickListener(this);
mButtonPlus.setOnClickListener(this);
mButtonMinus.setOnClickListener(this);
mButtonMultiply.setOnClickListener(this);
mButtonDivide.setOnClickListener(this);
mButtonPower.setOnClickListener(this);
mButtonSqrt.setOnClickListener(this);
mButtonEqual.setOnClickListener(this);
mButtonClear.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.button_0:
onNumberClicked("0");
break;
case R.id.button_1:
onNumberClicked("1");
break;
case R.id.button_2:
onNumberClicked("2");
break;
case R.id.button_3:
onNumberClicked("3");
break;
case R.id.button_4:
onNumberClicked("4");
break;
case R.id.button_5:
onNumberClicked("5");
break;
case R.id.button_6:
onNumberClicked("6");
break;
case R.id.button_7:
onNumberClicked("7");
break;
case R.id.button_8:
onNumberClicked("8");
break;
case R.id.button_9:
onNumberClicked("9");
break;
case R.id.button_plus:
onOperatorClicked("+");
break;
case R.id.button_minus:
onOperatorClicked("-");
break;
case R.id.button_multiply:
onOperatorClicked("*");
break;
case R.id.button_divide:
onOperatorClicked("/");
break;
case R.id.button_power:
onOperatorClicked("^");
break;
case R.id.button_sqrt:
onOperatorClicked("sqrt");
break;
case R.id.button_equal:
onEqualClicked();
break;
case R.id.button_clear:
onClearClicked();
break;
}
}
private void onNumberClicked(String number) {
if (mOperator.isEmpty()) {
mNumber1 += number;
mDisplayText.setText(mNumber1);
} else {
mNumber2 += number;
mDisplayText.setText(mNumber2);
}
}
private void onOperatorClicked(String operator) {
if (!mNumber1.isEmpty() && mNumber2.isEmpty()) {
mOperator = operator;
mDisplayText.setText(mNumber1 + " " + operator + " ");
}
}
private void onEqualClicked() {
double result = 0.0;
if (!mNumber1.isEmpty() && !mNumber2.isEmpty()) {
double num1 = Double.parseDouble(mNumber1);
double num2 = Double.parseDouble(mNumber2);
switch (mOperator) {
case "+":
result = num1 + num2;
break;
case "-":
result = num1 - num2;
break;
case "*":
result = num1 * num2;
break;
case "/":
result = num1 / num2;
break;
case "^":
result = Math.pow(num1, num2);
break;
}
mDisplayText.setText(mDecimalFormat.format(result));
mNumber1 = mDecimalFormat.format(result);
mNumber2 = "";
mOperator = "";
} else if (!mNumber1.isEmpty() && mNumber2.isEmpty() && mOperator.equals("sqrt")) {
double num1 = Double.parseDouble(mNumber1);
result = Math.sqrt(num1);
mDisplayText.setText(mDecimalFormat.format(result));
mNumber1 = mDecimalFormat.format(result);
mNumber2 = "";
mOperator = "";
}
}
private void onClearClicked() {
mNumber1 = "";
mNumber2 = "";
mOperator = "";
mDisplayText.setText("");
}
}
```
在这个示例代码中,我们实现了加、减、乘、除、乘方、开方等基本算术运算,并且使用 DecimalFormat 对结果进行格式化。当用户点击数字按钮时,我们将数字添加到当前操作数中;当用户点击操作符按钮时,我们将当前操作数视为第一个操作数,并将操作符保存;当用户点击等号按钮时,我们计算结果并在 TextView 中显示;当用户点击清除按钮时,我们将所有操作数和操作符都重置为初始状态。
最后,运行应用程序并测试计算器的功能。
阅读全文