欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java实验接口实现计算器功能(很水)

程序员文章站 2022-06-19 19:30:58
...

自己闲着就写了一下,纪念纪念

我写的类比较多,

import java.util.Scanner;

public class Jisuanqi {
    public static void main(String[] args) {
        double a,b;
        char x;
        System.out.println("切记输入的数字与符号要留出空格要不然会报错的");
        while(true) {
            Scanner cin = new Scanner(System.in);
            a = cin.nextDouble();
            x = cin.next().charAt(0);
            b = cin.nextDouble();
            Usefuhao u = new Usefuhao();
            u.start(x, a, b);
        }
    }
}

public class Usefuhao {
    public void start(char x,double a,double b){
        switch (x){
            case '+':
                jia j = new jia();
                System.out.println("a + b = "+j.yunxin(a,b));
                break;
            case '-':
                jian jan = new jian();
                jan.yunxin(a,b);
                System.out.println("a - b = "+jan.yunxin(a,b));
                break;
            case '*':
                chen c = new chen();
                System.out.println("a * b = "+c.yunxin(a,b));
                break;
            case '/':
                chu cu = new chu();
                System.out.println("a / b = "+cu.yunxin(a,b));
                break;
                default:
                    System.out.println("您输入的符号不在我的计算范围之内");
                    break;
        }
    }
}


public abstract interface fuhao {
    public abstract double yunxin(double n,double b);
}

public class jia implements fuhao {
    public double yunxin(double a,double b) {
        return a+b;
    }
}
public class jian implements fuhao{
    public double yunxin(double a,double b){
        return  a-b;
    }
}
public class chen implements fuhao{
    public double yunxin(double a,double b){
        return a*b;
    }
}
public class chu implements fuhao{
    public double yunxin(double a,double b){
        return a/b;
    }
}

java实验接口实现计算器功能(很水)