1 つのアイデアは、より機能的なスタイルを使用して、冗長な演算子と醜いケース スイッチを削減し、コードをより保守しやすくすることです。
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);
すべての操作を簡単に繰り返したり、名前を取得したりできます。