每天都在头秃 2022-12-07 13:46 采纳率: 96.7%
浏览 34
已结题

Java程序的异常不会解决

问题遇到的现象和发生背景 我在试着写一个可以将字符串转化为一个计算时,程序遇到如下问题

但我发现输入不同数据会产生不同的问题

用代码块功能插入代码,请勿粘贴截图
import java.util.*;

public class TestMathExper_122090080 {
    public static String A(String str) {// 预处理字符串
        String str0 = str.replace(" ", "");
        String str1 = str0.toString();
        char a = str1.charAt(1);
        if (a == '-') {
             String k =  "0" + str1;
             return k;
        } else {
            return str1;
        }

    }

    public static String removekuohao(String str){//去括号
        String a1 = str;
        if(str.contains("[(]")){
            int x0 =str.lastIndexOf("(");
            String y0 = str.substring(x0+1,a1.length());
            int x1 =y0.indexOf(")");
            String y1 = y0.substring(1, x1);
            String p0 = Sanjiao(y1);
            String final1;
            if(p0.contains("[(]")){
                String p1 =p0.replace("(", "").replace(")","");
                String p2 = Chengchu(p1);
                if(p2.contains("[(]")){
                    String p3 =p2.replace("(", "").replace(")","");
                    String p4 = Jiajian(p3);
                    if(p4.contains("[(]")){
                        final1 =p4.replace("(", "").replace(")","");
                     }
                    else{
                        final1 =p4;
                    }}
                else{
                    String p3 =Jiajian(p2);
                    if(p3.contains("[(]")){
                        final1 =p3.replace("(", "").replace(")","");
                     }
                    else{
                        final1 =p3;
                    }
                }
                }
            else {
                String p1 =Chengchu(p0);
                if(p1.contains("[(]")){
                    String p2 =p1.replace("(", "").replace(")","");
                    String p3 = Jiajian(p2);
                    if(p3.contains("[(]")){
                        final1 =p3.replace("(", "").replace(")","");
                     }
                    else{
                        final1 =p3;
                    }}
                else{
                    String p2 =Jiajian(p1);
                    if(p2.contains("[(]")){
                        final1 =p2.replace("(", "").replace(")","");
                     }
                    else{
                        final1 =p2;
                    }
                }

            }
            int Midlength = x0 + y1.length() +2;
            String Mid = str.substring(x0,Midlength);
            String str2 =str.replaceAll(Mid, final1);
            String begin =str2.toString();
            return removekuohao(begin) ;
        }
        return a1;


    }

    public static String Chengchu(String str) {// 处理乘除号
        String a2 = str;
        //分割字符串
        String re =A(str);
        int x = 0,y =0,z=0,k =0,x0 =0,y0 =0;
        char ai;
        String[] b0 = re.split("[*/+-]");//数字串
        String b1 =re.replaceAll("\\d","").replace(".","");
        if(re.contains("*")||re.contains("/")){
        if(re.contains("*")&&re.contains("/")){
            x = b1.indexOf("*");
            y = b1.indexOf("/");
            x0 =re.indexOf("*");
            y0 =re.indexOf("/");
            if(x > y){//除号在前
                 ai = re.charAt(y0+1);
                if(ai == '-'){
                    z=b1.indexOf("/");
                    k =re.indexOf("/");
                    b0[z+1] ="-"+b0[z+1]; 
                }
                else{z = b1.indexOf("/");
                k = re.indexOf("/");}
            }
            else if(y > x){//乘号在前
                ai = re.charAt(x0+1);
                if(ai == '-'){
                    z=b1.indexOf("*");
                    k =re.indexOf("*");
                    b0[z+1] ="-"+b0[z+1]; 
                }
                else{z = b1.indexOf("*");
                k = re.indexOf("*"); 
                b0[z+1] =b0[z+1];
                }       
            }
        }
        else if(re.contains("*")){
            ai = re.charAt(x0+1);
                if(ai == '-'){
                    z=b1.indexOf("*");
                    k =re.indexOf("*");
                    b0[z+1] ="-"+b0[z+1]; 
                }
            else{z = b1.indexOf("*");
            k = re.indexOf("*");
            b0[z+1] =b0[z+1];}
        }
        else if(re.contains("/")){
            ai = re.charAt(y0+1);
                if(ai == '-'){
                    z=b1.indexOf("/");
                    k =re.indexOf("/");
                    b0[z+1] ="-"+b0[z+1]; 
                }
            else{z = b1.indexOf("/");
            k = re.indexOf("/");
            b0[z+1] =b0[z+1];}
        }
        String smallequation = b0[z]+b1.charAt(z)+b0[z+1];
        String result1 = suanshi(smallequation);//获得结果
        int be = k - b0[z].length(), end =k+b0[z+1].length()+1;
        String equation1 = re.substring(be, end);
        String u = re.replaceAll(equation1, result1);
        return u;
        }
        return a2;
    }

    public static String Jiajian(String str) {// 处理加减
        String a3 = str;
        if(str.contains("+")||str.contains("-")){
            String fuck;
            if (str.contains("+-")||str.contains("--")){
                fuck =str.replaceAll("+-","-").replaceAll("--","+");
            }
            else{
                fuck =str;

            }
            String fuck1 =A(fuck);
            String[] l0 = fuck1.split("[*/+-]");//数字串
            String l1 =fuck1.replaceAll("\\d","").replace(".","");
            int l = 0,m =0,n=0,v =0;
            if(fuck1.contains("+")&&fuck1.contains("-")){
                l = l1.indexOf("+");
                m = l1.indexOf("-");
                if(l > m){//减号在前
                    n = m;
                    v = fuck1.indexOf("-");
                }
                else if(m > l){//乘号在前
                    n = l;
                    v = fuck1.indexOf("+");        
                }
            }
            else if(fuck.contains("+")){
                n = l1.indexOf("+");
                v = fuck1.indexOf("+");
            }
            else if(fuck.contains("-")){
                n = l1.indexOf("-");
                v = fuck1.indexOf("-");
            }
            String smallequation1 = l0[n]+l1.charAt(n)+l0[n+1];
            String result2 = suanshi(smallequation1);//获得结果
            int be1 = v - l0[n].length(), end1 =v+l0[n+1].length();
            String equation2 = str.substring(be1, end1+1);
            String u1 = str.replaceAll(equation2, result2);
            return u1;
            }
        return a3;
    }

    public static String Sanjiao(String str) {// 处理三角函数
        String a4 = str;
        if(str.contains("sin")||str.contains("cos")||str.contains("tan")||str.contains("sqrt")){//判断是否含有
            String[] r0 = str.split("[*/+-]");
            String r1 =str.replaceAll("\\d","").replace(".","");
            int j=0;
            while(j<r1.length()+1){
                double value =0.0d;
                if(r0[j].contains("sin")){
                    String t0 =r0[j].substring(3);
                        double s0 = Double.parseDouble(t0);
                         value = Math.sin(s0);
                    }
                     
                else if(r0[j].contains("cos")){
                    String t0 =r0[j].substring(3);
                        double s0 = Double.parseDouble(t0);
                         value = Math.cos(s0);
                }
                else if(r0[j].contains("tan")){
                    String t0 =r0[j].substring(3);
                        double s0 = Double.parseDouble(t0);
                         value = Math.tan(s0);
                }
                else if(r0[j].contains("sqrt")){
                    String t0 =r0[j].substring(4);
                        double s0 = Double.parseDouble(t0);
                        value = Math.sqrt(s0);
 
                }
                j++;
                String f ="("+ value+")";
                String equation3 =str.replaceAll(r0[j], f);
                return equation3;
             }  
        }return a4;

    }
    public static String suanshi(String str){
        double c =0.0d;
        String[] w0;
        String ko;
        if(str.contains("+")){
          w0 =str.split("+");
          double g1 =Double.parseDouble(w0[0]);
          double g2 =Double.parseDouble(w0[1]);
          c =g1 +g2;
        }
        else if(str.contains("-")){
          char first = str.charAt(0);
          if(first == '-'){
           ko = "0" + str;
           w0 = ko.split("-");
           double g0 =Double.parseDouble(w0[0]);
           double g1 =Double.parseDouble(w0[1]);
           double g2 =Double.parseDouble(w0[2]);
           c =g0 - g1 - g2;
          }
          else{
          w0 =str.split("-");
          double g1 =Double.parseDouble(w0[0]);
          double g2 =Double.parseDouble(w0[1]);
          c =g1 -g2;}
        }
        else if(str.contains("*")){
          w0 =str.split("[*]");
          double g1 =Double.parseDouble(w0[0]);
          double g2 =Double.parseDouble(w0[1]);
          c =g1 *g2;
        }
        else if(str.contains("/")){
          w0 =str.split("/");
          double g1 =Double.parseDouble(w0[0]);
          double g2 =Double.parseDouble(w0[1]);
          c =g1 /g2;
        }
        String d = "(" + c + ")";
        return d;

    }

    public static double parse(String str) {// 得到结果
        String yuchuli =A(str);
        String wukohao = removekuohao(yuchuli);
        String w = Sanjiao(wukohao);
        String w1 = removekuohao(w);
        String w2 = Chengchu(w1);
        String w3 =removekuohao(w2);
        String w4 =Jiajian(w3);
        String w5 =removekuohao (w4);
        double o =Double.parseDouble(w5);
        return o;
 

    }

    public static void main(String[] args) throws Exception {
        Scanner input = new Scanner(System.in);
        while (input.hasNextLine()) {
            double result = parse(input.nextLine());
            System.out.println(String.valueOf(Math.round(result)));
        }
        input.close();
    }
}

运行结果及报错内容

1.2-3.5*5.2-13.7
Exception in thread "main" java.lang.NumberFormatException: For input string: "2.3)"
at java.base/jdk.internal.math.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2054)
at java.base/jdk.internal.math.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
at java.base/java.lang.Double.parseDouble(Double.java:651)
at TestMathExper_122090080.suanshi(TestMathExper_122090080.java:254)
at TestMathExper_122090080.Jiajian(TestMathExper_122090080.java:185)
at TestMathExper_122090080.Jiajian(TestMathExper_122090080.java:189)
at TestMathExper_122090080.parse(TestMathExper_122090080.java:282)
at TestMathExper_122090080.main(TestMathExper_122090080.java:293)

-3+4/ 2.5+3.7
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 2 out of bounds for length 2
at TestMathExper_122090080.suanshi(TestMathExper_122090080.java:250)
at TestMathExper_122090080.Jiajian(TestMathExper_122090080.java:186)
at TestMathExper_122090080.parse(TestMathExper_122090080.java:283)
at TestMathExper_122090080.main(TestMathExper_122090080.java:294)

我的解答思路和尝试过的方法 我独自debug 感到有点困难故寻求帮助,代码很长实在是麻烦了
我想要达到的结果
  • 写回答

3条回答 默认 最新

  • 心寒丶 全栈领域优质创作者 2022-12-07 13:51
    关注

    你输入的是什么数据呢,不是提醒你在字符串转为数字的时候出错了么

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 12月15日
  • 已采纳回答 12月7日
  • 创建了问题 12月7日

悬赏问题

  • ¥100 求购一套带接口实现实习自动签到打卡
  • ¥50 MacOS 使用虚拟机安装k8s
  • ¥500 亚马逊 COOKIE我如何才能实现 登录一个亚马逊账户 下发新 COOKIE ..我使用下发新COOKIE 导入ADS 指纹浏览器登录,我把账户密码 修改过后,原来下发新COOKIE 不会失效的方式
  • ¥20 玩游戏gpu和cpu利用率特别低,玩游戏卡顿
  • ¥25 oracle中的正则匹配
  • ¥15 关于#vscode#的问题:把软件卸载不会再出现蓝屏
  • ¥15 vimplus出现的错误
  • ¥15 usb无线网卡转typec口
  • ¥30 怎么使用AVL fire ESE软件自带的优化模式来优化设计Soot和NOx?
  • ¥15 Ubuntu20.04.4.LTS系统如何下载安装VirtualBox虚拟机?