zls_csdn 2024-03-25 11:01 采纳率: 0%
浏览 4

Android studio中有报错,如何解决?

第23行之后的R一直报错,不知道什么问题


package com.zdb.hwfrist;

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_clear.setOnClickListener(v -> {
            tv_result.setText(" ");
        });
        btn_none.setOnClickListener(v -> {
            tv_result.setText("叫你手贱,点我干什么");
        });
        btn_del.setOnClickListener(v -> {
            String str_out = tv_result.getText().toString();
            if (str_out.length() == 1) {
                tv_result.setText(" ");
            } else {
                tv_result.setText(str_out.substring(0, str_out.length() - 1));
            }
        });
        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_equal.setOnClickListener(v -> {
            String str = tv_result.getText().toString();
//            如果str是中文,就不执行
            if (str.contains("叫")) {
                return;
            }
//            如果str是空的,就不执行
            if (str.equals(" ")) {
                return;
            }
//            如果str是以运算符结尾,就不执行
            if (str.endsWith("+") || str.endsWith("-") || str.endsWith("*") || str.endsWith("/")) {
                tv_result.setText(str+"好好写");
                return;
            }
            str="1*"+str;
//            如果str有%,则在%后面加一个1
            if (str.contains("%")) {
                str = str.replace("%", "%0");
            }
            Object result = calculate(str);
            System.out.println(result);

//                打印result
            System.out.println(result);
//               如果小数点后面是0,就不显示
            if (result.toString().endsWith(".0")) {
                tv_result.setText(result.toString().substring(0, result.toString().length() - 2));
            } else {
                tv_result.setText(result + "");
            }


        });
    }

    public static double calculate(String input) {
        String[] parts = input.split("(?<=[-+*/%])|(?=[-+*/%])");
        Stack<Double> numbers = new Stack<>();
        Stack<Character> operators = new Stack<>();
        for (String part : parts) {
            part = part.trim();
            if (part.isEmpty()) {
                continue;
            }
            char firstChar = part.charAt(0);
            if (Character.isDigit(firstChar)) {
                double num = Double.parseDouble(part);
                numbers.push(num);
            } else {
                while (!operators.isEmpty() && shouldPopOperator(operators.peek(), firstChar)) {
                    char op = operators.pop();
                    double num2 = numbers.pop();
                    double num1 = numbers.pop();
                    double result = calculateOneOperation(num1, num2, op);
                    numbers.push(result);
                }
                operators.push(firstChar);
            }
        }
        while (!operators.isEmpty()) {
            char op = operators.pop();
            double num2 = numbers.pop();
            double num1 = numbers.pop();
            double result = calculateOneOperation(num1, num2, op);
            numbers.push(result);
        }
        return numbers.pop();
    }

    private static boolean shouldPopOperator(char op1, char op2) {
        if (op1 == '(' || op1 == ')') {
            return false;
        }
        int precedence1 = getOperatorPrecedence(op1);
        int precedence2 = getOperatorPrecedence(op2);
        return precedence1 >= precedence2;
    }

    private static int getOperatorPrecedence(char op) {
        switch (op) {
            case '+':
            case '-':
                return 1;
            case '*':
            case '/':
                return 2;
            case '%':
                return 3;
            default:
                return 0;
        }
    }

    private static double calculateOneOperation(double num1, double num2, char op) {
        switch (op) {
            case '%':
                return num1 * 0.01;
            case '+':
                return num1 + num2;
            case '-':
                return num1 - num2;
            case '*':
                return num1 * num2;
            case '/':
                return num1 / num2;

            default:
                throw new IllegalArgumentException("Invalid operator: " + op);
        }
    }
}

  • 写回答

2条回答 默认 最新

  • 码农阿豪@新空间 新星创作者: 前端开发技术领域 2024-03-25 11:01
    关注
    让阿豪来帮你解答,本回答参考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<Float> numStack = new Stack<>();
                Stack<Character> 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;
            }
        }
    }
    
    评论

报告相同问题?

问题事件

  • 创建了问题 3月25日