问题描述
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import com.shenchen.model.Item;import com.shenchen.type.Types;import com.shenchen.util.Calculator;public class Main {public static void main(String[] args) throws Exception, IOException {InputStreamReader isr = new InputStreamReader(System.in);BufferedReader br = new BufferedReader(isr);Item item = new Item();Double num1 = Double.parseDouble( br.readLine());item.setNum1(num1);System.out.println("请输入一个运算符号:");String str = br.readLine();switch (str) {case "+":item.setType(Types.JIA);break;case "-":item.setType(Types.JIAN);break;case "*":item.setType(Types.CHENG);break;case "/":item.setType(Types.CHU);break;default:System.out.println("符号错误!");System.exit(0);}System.out.println("请输入一个数字:");Double num2 = Double.parseDouble(br.readLine());item.setNum2(num2);new Calculator(item);}}public class Item {double num1;double num2;int type;public void setNum1(double num1) {this.num1 = num1;}public void setNum2(double num2) {this.num2 = num2;}public void setType(int type) {this.type = type;}public double getNum1() {return num1;}public double getNum2() {return num2;}public int getType() {return type;}}public class Types {public final static int JIA = 1;public final static int JIAN = 2;public final static int CHENG =3;public final static int CHU = 4;}import com.shenchen.model.Item;import com.shenchen.type.Types;public class Calculator {double result;public Calculator(Item item) {double num1 = item.getNum1();double num2 = item.getNum2();switch (item.getType()) {case Types.JIA:result = num1+num2;System.out.println(num1+"+"+num2+" = "+result);break;case Types.JIAN:result = num1-num2;System.out.println(num1+"-"+num2+" = "+result);break;case Types.CHENG:result = num1*num2;System.out.println(num1+"*"+num2+" = "+result);break;case Types.CHU:result = num1/num2;System.out.println(num1+"/"+num2+" = "+result);break;}}} 问题补充:如果不是,能不能指点一下。大概得怎么写。最好是贴出代码啦,学习学习
解决方案
将计算的抽一个接口出来ICalculator,具体计算实现该接口。如果要加功能,添加其它计算方法,添加实现类就好了,另外可以弄一个Factory负责创建这些Calculator。代码简单贴一下。public interface ICalculator {public double calculate(double ...args);}//加法public class PlusCalculator implements ICalculator {@Overridepublic double calculate(double... args) {double res = 0;for(double arg:args)res += arg;return res;}}//减法public class MinusCalculator implements ICalculator {@Overridepublic double calculate(double... args) {if (args.length == 0)return 0;double res = args[0];for (int i = 1; i < args.length; ++i) {res -= args[i];}return res;}}//平方public class SquareCalculator implements ICalculator {@Overridepublic double calculate(double... args) { //只接受一个参数if(args.length == 1)return args[0]*args[0];elsereturn 0;}}//操作类型public enum CalculatorType {PlusCalculator,MinusCalculator,SquareCalculator;}//工厂public class CalculatorFactory {public static ICalculator getCalculator(CalculatorType type) {ICalculator calculator = null;switch (type) {case MinusCalculator:calculator = new MinusCalculator();break;case PlusCalculator:calculator = new PlusCalculator();break;case SquareCalculator:calculator = new SquareCalculator();break;default:break;}return calculator;}}//测试代码public static void main(String[] args) {double arg1 = 100;double arg2 = 90;double arg3 = 80;ICalculator calculator = CalculatorFactory.getCalculator(CalculatorType.PlusCalculator);System.out.println(calculator.calculate(arg1,arg2,arg3));calculator = CalculatorFactory.getCalculator(CalculatorType.MinusCalculator);System.out.println(calculator.calculate(arg1,arg2,arg3));calculator = CalculatorFactory.getCalculator(CalculatorType.SquareCalculator);System.out.println(calculator.calculate(arg1));}
解决方案二:
这样写不好,应该用接口实现