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


    关注 码龄 粉丝数 原力等级 -- 被采纳 被点赞 采纳率 zls_csdn 2024-03-25 11:01 采纳率: 0% 浏览 4 首页/ 编程语言 / Android studio中有报错,如何解决? java 第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 numbers = new Stack<>(); Stack 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); } } } 展开全部 收起 写回答 好问题 0 提建议 追加酬金 关注问题 微信扫一扫 点击复制链接 分享 邀请回答 编辑 收藏 删除 结题 收藏 举报 追加酬金 (90%的用户在追加酬金后获得了解决方案) 当前问题酬金 ¥ 0 (可追加 ¥500) ¥ 15¥ 20¥ 50¥ 100¥ 200 支付方式 扫码支付 二维码出错 点击刷新 支付金额 15 元 提供问题酬金的用户不参与问题酬金结算和分配 支付即为同意 《付费问题酬金结算规则》 结题 再想想 删除 再等等

    2条回答 默认 最新

    • 码农阿豪 Java领域优质创作者 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 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;
              }
          }
      }
      

      展开全部

      评论
    • CSDN-Ada助手 CSDN-AI 官方账号 2024-03-25 12:02
      关注

      【相关推荐】




      如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^
      评论
    编辑
    预览

    报告相同问题?

  • 相关阅读:
    大模型技术实践(五)|支持千亿参数模型训练的分布式并行框架
    【环境搭建】linux docker-compose安装seata1.6.1,使用nacos注册、db模式
    【面试系列】Java面试知识篇(四)
    docker 部署hbase 并且java Api连接
    【HMS core】【Health Kit】【FAQ】数据订阅功能问题合集
    Android 11.0 framework层实现app默认全屏显示
    html 标签简介
    【Android Studio学习】第二篇、APP实现画简易的波形图
    git commit规范
    软件项目管理 6.8.专家估算法
  • 原文地址:https://ask.csdn.net/questions/8078532