String s = "12+45+33+5+89";
计算表示式的值
有几个加号不确定
如何算,需要代码学习。。谢谢

解决方案 »

  1.   

    可以用split将字符串拆开
    然后转成int型运算
      

  2.   


                    String s = "12+45+33+5+89";
    String[] nums = s.split("\\+");
    Integer sum = 0;
    for(String num : nums)
    sum += Integer.parseInt(num);
    System.out.println( "" + sum);
      

  3.   

    试下我的:public static void main(String[] args) {
    String s = "+12+23+34";
    String[] tempStr = s.trim().split("[+]");
    Double sum = 0.00;
    for (int i = 0; i < tempStr.length; i++) {
    sum += Double.parseDouble(tempStr[i].equals("")?"0.00":tempStr[i]);
    }
    System.out.println(sum);
    }
      

  4.   

    Pattern ptn = Pattern.compile("\\d+(?!\\d)");
    Matcher matcher = ptn.matcher("12+45+33+5+89");
    int total = 0;
    while(matcher.find()){
    total += Integer.parseInt(matcher.group());
    }
    System.out.println(total);
      

  5.   

    别人都写好了,而且写的非常好,用就是了。
    http://sourceforge.net/projects/jep/