安卓(Android studio)编写一个计算器,可以连续加减乘除,可加小数点,不包含括号以及加减乘除优先算法。
时间: 2023-06-16 07:04:47 浏览: 157
jisuanqi.rar_android_android 表格_jisuanqi.app
好的,以下是一个简单的安卓计算器示例代码,你可以在Android Studio中创建一个新项目并将其复制到MainActivity.java文件中:
```java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private TextView display; // 显示屏
private Button btn0, btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9; // 数字键
private Button btnAdd, btnSubtract, btnMultiply, btnDivide, btnDot, btnClear, btnEqual; // 运算键
private double num1 = 0, num2 = 0; // 存储两个操作数
private String operator = ""; // 存储当前运算符
private boolean isDot = false; // 记录是否输入过小数点
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 获取控件
display = findViewById(R.id.display);
btn0 = findViewById(R.id.btn0);
btn1 = findViewById(R.id.btn1);
btn2 = findViewById(R.id.btn2);
btn3 = findViewById(R.id.btn3);
btn4 = findViewById(R.id.btn4);
btn5 = findViewById(R.id.btn5);
btn6 = findViewById(R.id.btn6);
btn7 = findViewById(R.id.btn7);
btn8 = findViewById(R.id.btn8);
btn9 = findViewById(R.id.btn9);
btnDot = findViewById(R.id.btnDot);
btnAdd = findViewById(R.id.btnAdd);
btnSubtract = findViewById(R.id.btnSubtract);
btnMultiply = findViewById(R.id.btnMultiply);
btnDivide = findViewById(R.id.btnDivide);
btnClear = findViewById(R.id.btnClear);
btnEqual = findViewById(R.id.btnEqual);
// 设置点击事件监听器
btn0.setOnClickListener(this);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
btn4.setOnClickListener(this);
btn5.setOnClickListener(this);
btn6.setOnClickListener(this);
btn7.setOnClickListener(this);
btn8.setOnClickListener(this);
btn9.setOnClickListener(this);
btnDot.setOnClickListener(this);
btnAdd.setOnClickListener(this);
btnSubtract.setOnClickListener(this);
btnMultiply.setOnClickListener(this);
btnDivide.setOnClickListener(this);
btnClear.setOnClickListener(this);
btnEqual.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn0:
inputNumber(0);
break;
case R.id.btn1:
inputNumber(1);
break;
case R.id.btn2:
inputNumber(2);
break;
case R.id.btn3:
inputNumber(3);
break;
case R.id.btn4:
inputNumber(4);
break;
case R.id.btn5:
inputNumber(5);
break;
case R.id.btn6:
inputNumber(6);
break;
case R.id.btn7:
inputNumber(7);
break;
case R.id.btn8:
inputNumber(8);
break;
case R.id.btn9:
inputNumber(9);
break;
case R.id.btnDot:
inputDot();
break;
case R.id.btnAdd:
operator = "+";
num1 = Double.parseDouble(display.getText().toString());
display.setText("");
isDot = false;
break;
case R.id.btnSubtract:
operator = "-";
num1 = Double.parseDouble(display.getText().toString());
display.setText("");
isDot = false;
break;
case R.id.btnMultiply:
operator = "*";
num1 = Double.parseDouble(display.getText().toString());
display.setText("");
isDot = false;
break;
case R.id.btnDivide:
operator = "/";
num1 = Double.parseDouble(display.getText().toString());
display.setText("");
isDot = false;
break;
case R.id.btnClear:
num1 = 0;
num2 = 0;
operator = "";
display.setText("");
isDot = false;
break;
case R.id.btnEqual:
if (!operator.equals("")) {
num2 = Double.parseDouble(display.getText().toString());
double result = 0;
switch (operator) {
case "+":
result = num1 + num2;
break;
case "-":
result = num1 - num2;
break;
case "*":
result = num1 * num2;
break;
case "/":
if (num2 != 0) {
result = num1 / num2;
} else {
result = 0;
}
break;
}
display.setText(String.valueOf(result));
num1 = result;
isDot = false;
}
break;
}
}
// 输入数字
private void inputNumber(int num) {
String text = display.getText().toString();
if (text.equals("0")) {
text = "";
}
display.setText(text + num);
}
// 输入小数点
private void inputDot() {
if (!isDot) {
String text = display.getText().toString();
if (text.equals("")) {
text = "0";
}
display.setText(text + ".");
isDot = true;
}
}
}
```
此示例代码只包含加减乘除和小数点功能,不包含括号和加减乘除的优先级规则。如果需要实现更复杂的计算器功能,可以参考其他示例代码或自行开发。
阅读全文