让阿豪来帮你解答,本回答参考chatgpt3.5编写提供,如果还有疑问可以评论或留言
这段代码中出现了多处错别字,包括但不限于:
- 第1行和第2行中应该是 "<" 和 "/" 符号的闭合。
- 第23行中的 R 没有定义,可能是忘记导入 R 类。
- 每个 View 对象的实例化语句末尾都应该有分号。
- 第55行(OnClickListener)和第87行(onItemClick)的箭头符号应该改为 "->"。
- 第76行字符串拼接时应该加上加号 "+"。 以下是修改后的示例代码:
package com.zdb.hwfirst;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import java.util.Stack;
public class MainActivity extends AppCompatActivity {
// 创建button对象
Button btn_none, btn_0, btn_1, btn_2, btn_3, btn_4, btn_5, btn_6, btn_7, btn_8, btn_9, btn_percent, btn_del, btn_point, btn_add, btn_sub, btn_mul, btn_div, btn_equal, btn_clear;
// 创建TextView对象
TextView tv_result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 实例化对象
btn_0 = findViewById(R.id.btn_0);
btn_1 = findViewById(R.id.btn_1);
btn_2 = findViewById(R.id.btn_2);
btn_3 = findViewById(R.id.btn_3);
btn_4 = findViewById(R.id.btn_4);
btn_5 = findViewById(R.id.btn_5);
btn_6 = findViewById(R.id.btn_6);
btn_7 = findViewById(R.id.btn_7);
btn_8 = findViewById(R.id.btn_8);
btn_9 = findViewById(R.id.btn_9);
btn_percent = findViewById(R.id.btn_percent);
btn_add = findViewById(R.id.btn_add);
btn_sub = findViewById(R.id.btn_sub);
btn_mul = findViewById(R.id.btn_mul);
btn_div = findViewById(R.id.btn_div);
btn_del = findViewById(R.id.btn_del);
btn_equal = findViewById(R.id.btn_equal);
btn_point = findViewById(R.id.btn_point);
btn_clear = findViewById(R.id.btn_clear);
tv_result = findViewById(R.id.tv_result);
btn_none = findViewById(R.id.btn_none);
// 给按钮添加点击事件
btn_0.setOnClickListener(v -> {
String str_out = tv_result.getText().toString();
tv_result.setText(str_out + "0");
});
btn_1.setOnClickListener(v -> {
String str_out = tv_result.getText().toString();
tv_result.setText(str_out + "1");
});
btn_2.setOnClickListener(v -> {
String str_out = tv_result.getText().toString();
tv_result.setText(str_out + "2");
});
btn_3.setOnClickListener(v -> {
String str_out = tv_result.getText().toString();
tv_result.setText(str_out + "3");
});
btn_4.setOnClickListener(v -> {
String str_out = tv_result.getText().toString();
tv_result.setText(str_out + "4");
});
btn_5.setOnClickListener(v -> {
String str_out = tv_result.getText().toString();
tv_result.setText(str_out + "5");
});
btn_6.setOnClickListener(v -> {
String str_out = tv_result.getText().toString();
tv_result.setText(str_out + "6");
});
btn_7.setOnClickListener(v -> {
String str_out = tv_result.getText().toString();
tv_result.setText(str_out + "7");
});
btn_8.setOnClickListener(v -> {
String str_out = tv_result.getText().toString();
tv_result.setText(str_out + "8");
});
btn_9.setOnClickListener(v -> {
String str_out = tv_result.getText().toString();
tv_result.setText(str_out + "9");
});
btn_point.setOnClickListener(v -> {
String str_out = tv_result.getText().toString();
tv_result.setText(str_out + ".");
});
btn_add.setOnClickListener(v -> {
String str_out = tv_result.getText().toString();
tv_result.setText(str_out + "+");
});
btn_sub.setOnClickListener(v -> {
String str_out = tv_result.getText().toString();
tv_result.setText(str_out + "-");
});
btn_mul.setOnClickListener(v -> {
String str_out = tv_result.getText().toString();
tv_result.setText(str_out + "*");
});
btn_div.setOnClickListener(v -> {
String str_out = tv_result.getText().toString();
tv_result.setText(str_out + "/");
});
btn_percent.setOnClickListener(v -> {
String str_out = tv_result.getText().toString();
tv_result.setText(str_out + "%");
});
btn_del.setOnClickListener(v -> {
String str_out = tv_result.getText().toString();
if (str_out.length() > 0) {
tv_result.setText(str_out.substring(0, str_out.length() - 1));
}
});
btn_clear.setOnClickListener(v -> tv_result.setText(""));
btn_equal.setOnClickListener(v -> {
String str_out = tv_result.getText().toString();
Stack numStack = new Stack<>();
Stack opStack = new Stack<>();
String num = "";
for (int i = 0; i < str_out.length(); i++) {
char c = str_out.charAt(i);
if (Character.isDigit(c) || c == '.') {
num += c;
} else {
if (!num.isEmpty()) {
numStack.push(Float.parseFloat(num));
num = "";
}
while (!opStack.isEmpty() && priority(opStack.peek()) >= priority(c)) {
float b = numStack.pop();
float a = numStack.pop();
numStack.push(operate(a, b, opStack.pop()));
}
opStack.push(c);
}
}
if (!num.isEmpty()) {
numStack.push(Float.parseFloat(num));
}
while (!opStack.isEmpty()) {
float b = numStack.pop();
float a = numStack.pop();
numStack.push(operate(a, b, opStack.pop()));
}
tv_result.setText(String.valueOf(numStack.pop()));
});
}
private int priority(char op) {
switch (op) {
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '%':
return 3;
default:
return 0;
}
}
private float operate(float a, float b, char op) {
switch (op) {
case '+':
return a + b;
case '-':
return a - b;
case '*':
return a * b;
case '/':
if (b == 0) {
return Float.NaN;
} else {
return a / b;
}
case '%':
return a % b;
default:
return 0;
}
}
}