Caculater_2.1

接口

package caculater1.inter;

public interface Computing {
    public double res(double a,double b)
/*2.0的有问题版本
    public static double res(double a,double b) {
        return 0;
    }
*/
}

Map

import java.util.HashMap;
import caculater1.computing.*;
import caculater1.inter.Computing;

public class Compute {
    @SuppressWarnings("serial")
    public static HashMap map = new HashMap() {
        {
            put("+", new Add());
            put("-", new Sub());
            put("*", new Mul());
            put("/", new Div());
        }
    };
    public static Computing getOperator(String operator) {
        return map.get(operator);
    }
}

计算

package caculater1.computing;
import caculater1.inter.Computing;

public class Add implements Computing{
    public double res(double a, double b) {
        return a+b;
    }
}
public class Sub implements Computing{
    public double res(double a, double b) {
        return a-b;
    }
}
public class Mul implements Computing{
    public double res(double a, double b) {
        return a*b;
    }
}
public class Div implements Computing{
//    @SuppressWarnings("null")
    public double res(double a, double b) {
        return a/b;
/*        try {
            return a/b;
        } catch (Exception e) {
            System.out.println("Division by Zero");这一行并不会被显示
            return (Double) null;
java中除Double(0)并不会抛出异常,而是返回Infinity,但try-catch对此仍然有效;return null会返回NaN(Not a Number)。
*/
        }
    }
}

输入

package caculater1;
import java.util.Scanner;

public class ComputerScanner {
    public static Object Inp(){
        String str;
        Double n;
        @SuppressWarnings("resource")
        Scanner sc = new Scanner(System.in);
        str = sc.nextLine();
        try{
            n = Double.parseDouble(str);
            return n;
        }catch (Exception e){
            return Compute.getOperator(str);
        }
    }
}

主程序

package caculater1;
import caculater1.inter.Computing;

public class Caculater1 {
    public static void main(String[] args) {
        Double res = 0.0;
        Computing ope = null;
        while(true){
            Object obj = ComputerScanner.Inp();
            if(obj==null){
                continue;
            }else{
                if(ope==null){
                    if(obj instanceof Double){
                        res = (Double) obj;
                    }else if(obj instanceof Computing){
                        ope =(Computing) obj;
                    }else{
                        System.out.println("Error input");
                    }
                }else{
                    if(obj instanceof Double){
                        Double num = (Double) obj;
                        res = ope.res(res,num);
//                        res = Computing.res(res, num);2.0中的错误
                        System.out.println("Result: "+res);
                        ope = null;//2.0中未加入这一行导致运行情况与预期不符
                    }else if(obj instanceof Computing){
                        ope = (Computing) obj;
                    }else{
                        System.out.println("Error input");
                    }
                }
            }
        }
    }
}

使用debug排除了2.0版本的错误
2.0版本bug
1.没有新建一个Computing的对象,直接调用Computing,按照eclipse提示修改Computing为static。导致后续问题
2.主程序设计思路漏洞

你可能感兴趣的:(Caculater_2.1)