用java实现计算器四则运算以及混合运算

贴代码
本例测试是基于junit ,eclipse可安装对应 的java包
我用的是idea,添加插件即可

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class Calculator {

    public  double add(String a, String  b) {
        double c=0;
        try {
            Integer.parseInt(a);
            Integer.parseInt(b);
           c=Integer.parseInt(a)+Integer.parseInt(b);
           System.out.println("正在计算");
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }finally {
            System.out.println("请输入正确的数字");
        }
        return c;
    }

    public double substract(String a, String b) {
        double c=0;
        try {
            Integer.parseInt(a);
            Integer.parseInt(b);
            c=Integer.parseInt(a)-Integer.parseInt(b);
            System.out.println("正在计算");
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }finally {
            System.out.println("请输入正确的数字");
        }
        return c;
    }

    public double multiply(String a, String b) {
        double c=0;
        try {
            Integer.parseInt(a);
            Integer.parseInt(b);
            c=Integer.parseInt(a)*Integer.parseInt(b);
            System.out.println("正在计算");
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }finally {
            System.out.println("请输入正确的数字");
        }
        return c;
    }

    public double divide(String a, String b) {
        double c=0;
        try {
            Integer.parseInt(a);
            Integer.parseInt(b);
            c=Integer.parseInt(a)/(double)Integer.parseInt(b);
            System.out.println("正在计算");
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }finally {
            System.out.println("请输入正确的数字");
        }
        return c;
    }

    public  float mixOpt(String s) throws Exception {
            if((s.equals("")||s.equals(null)))
                throw new Exception("请输入正确的表达式");
           if (s.endsWith("+")||s.endsWith("-")||s.endsWith("*")||s.endsWith("/")){
               throw new Exception("请输入正确的表达式");
           }
        if (s == null || "".equals(s.trim())) {  //trim用于删除字符串左右两边的空格
            return 0f;  //方法的返回类型为float,0f代表单精度的0.
        }
        //indexOf 方法返回一个整数值,指出 String 对象内子字符串的开始位置。如果没有找到子字符串,则返回-1。
        int a1 = s.indexOf("+");
        int a2 = s.indexOf("-");
        int a3 = s.indexOf("*");
        int a4 = s.indexOf("/");
        int a5 = s.indexOf("(");
        if (a1 == -1 && a2 == -1 && a3 == -1 && a4 == -1) {

            if (s.trim() == null || "".equals(s.trim())) {
                throw new Exception("operate error");
            }
            return Float.parseFloat(s.trim());
        }

        if (a5 != -1) {
            int a6 = s.indexOf(")");
            if (a6 == -1) {
                throw new Exception("括号不匹配");
            } else {

                float f = mixOpt(s.substring(a5 + 1, a6).trim());
                s = s.replace(s.substring(a5, a6 + 1), String.valueOf(f)); //使用第二个参数替换第一个参数
                //String.valueOf(float f) : 将 float 变量 f 转换成字符串
                return mixOpt(s);
            }
        }

        if (a1 != -1) {
            return mixOpt(s.substring(0, a1)) + mixOpt(s.substring(a1 + 1, s.length()));
        }
        if (a2 != -1) {
            return mixOpt(s.substring(0, a2)) - mixOpt(s.substring(a2 + 1, s.length()));
        }
        if (a3 != -1) {
            return mixOpt(s.substring(0, a3)) * mixOpt(s.substring(a3 + 1, s.length()));
        }
        if (a4 != -1) {
            return mixOpt(s.substring(0, a4)) / mixOpt(s.substring(a4 + 1, s.length()));
        }
        return Integer.parseInt(s.trim());
    }
}


测试类

import org.junit.jupiter.api.Test;

import java.util.Scanner;

import static org.junit.jupiter.api.Assertions.*;

class CalculatorTest {

    @Test
    void add( ) {
        System.out.println("请输入两个要运算的数字");
        String a="9";
        String  b="6";
     double c=  new Calculator().add(a,b);
        System.out.println(c);
        System.out.println("新的测试用例");
        a="a";
        b="9";
        c=  new Calculator().add(a,b);
        System.out.println(c);
    }

    @Test
    void substract() {
        System.out.println("请输入两个要运算的数字");
        String a="9";
        String  b="6";
        double c=  new Calculator().substract(a,b);
        System.out.println(c);
        System.out.println("新的测试用例");
        a="a";
        b="9";
        c=  new Calculator().add(a,b);
        System.out.println(c);
    }

    @Test
    void multiply() {
        System.out.println("请输入两个要运算的数字");
        String a="9";
        String  b="6";
        double c=  new Calculator().multiply(a,b);
        System.out.println(c);
        System.out.println("新的测试用例");
        a="a";
        b="9";
        c=  new Calculator().add(a,b);
        System.out.println(c);
    }

    @Test
    void divide() {
        System.out.println("请输入两个要运算的数字");
        String a="9";
        String  b="6";
        double c=  new Calculator().divide(a,b);
        System.out.println(c);
        System.out.println("新的测试用例");
        a="a";
        b="9";
        c=  new Calculator().add(a,b);
        System.out.println(c);
    }

    @Test
    void mixOpt() throws Exception {
        String s="6+7-4*2";
        double c=new Calculator().mixOpt(s);
        System.out.println(c);
        System.out.println();
         s="65+";
         c=new Calculator().mixOpt(s);
        System.out.println(c);
    }
}
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

凌晨里的无聊人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值