一个想法是使用更多的函数式风格来减少冗余的运算符和丑陋的大小写开关,并使代码更易于维护:
import java.util.*;
public class apples {
protected static final Map<String, BinOp> operators = new HashMap<String, BinOp>() {{
put("+", new BinOp() { public double calc(double op1, double op2) { return op1 + op2; }; });
put("-", new BinOp() { public double calc(double op1, double op2) { return op1 - op2; }; });
put("x", new BinOp() { public double calc(double op1, double op2) { return op1 * op2; }; });
put("/", new BinOp() { public double calc(double op1, double op2) { return op1 / op2; }; });
}};
public static void main(String args[]){
Scanner calculator = new Scanner(System.in);
double fnum, snum, answer;
System.out.println("Enter first number: ");
fnum = calculator.nextDouble();
System.out.println("Enter second number: ");
snum = calculator.nextDouble();
System.out.println("enter any operator: ");
String op = calculator.next();
BinOp opFunction = operators.get(op);
answer = opFunction.calc(fnum, snum);
System.out.println(answer);
}
}
interface BinOp {
double calc(double op1, double op2);
}
当然,您不应该在输入或不存在的运算符中处理双精度值。
另一个好主意是分离逻辑:
import java.util.*;
public class apples {
public static void main(String args[]){
Scanner calculator = new Scanner(System.in);
double fnum, snum, answer;
System.out.println("Enter first number: ");
fnum = calculator.nextDouble();
System.out.println("Enter second number: ");
snum = calculator.nextDouble();
System.out.println("enter any operator: ");
String op = calculator.next();
answer = calc(op, fnum, snum);
System.out.println(answer);
}
public static double calc(String op, double op1, double op2) {
switch (op) {
case ("+"): return op1 + op2;
case ("-"): return op1 - op2;
case ("x"): return op1 * op2;
case ("/"): return op1 / op2;
}
throw new RuntimeException("Not implemented!");
}
}
它使您的代码更具可读性和可维护性。
因为我真的很喜欢枚举:
import java.util.*;
public class apples {
public static void main(String args[]){
Scanner calculator = new Scanner(System.in);
double fnum, snum, answer;
System.out.println("Enter first number: ");
fnum = calculator.nextDouble();
System.out.println("Enter second number: ");
snum = calculator.nextDouble();
System.out.println("enter any operator: ");
String op = calculator.next();
Operation operator = Operation.get(op);
answer = operator.calc(fnum, snum);
System.out.println(answer);
}
}
enum Operation {
ADD("+") {
public double calc(double op1, double op2) {
return op1 + op2;
}
},
SUB("-") {
public double calc(double op1, double op2) {
return op1 - op2;
}
},
MUL("x") {
public double calc(double op1, double op2) {
return op1 * op2;
}
},
DIV("/") {
public double calc(double op1, double op2) {
return op1 / op2;
}
},
;
Operation(String op) {
this.op = op;
}
protected String op;
public abstract double calc(double op1, double op2);
public static Operation get(String op) {
for (Operation operation : values()) {
if (operation.op.equals(op)) {
return operation;
}
}
throw new RuntimeException("Not implemented!");
}
}
您也可以在其他地方使用:
answer = Operation.MUL(2, 3);
并轻松迭代所有操作,获取名称等。