我正在制作一个计算器Gui。有没有办法让这个代码看起来更干净,没有重复行。
double num1, num2, ans, doub;
num1 = Double.parseDouble(FirstInput.getText());
num2 = Double.parseDouble(SecondInput.getText());
oper = (Operator.getText());
if(oper.equals("+")){
ans = num1 + num2;
doub = Math.round(ans * 100000.0) / 100000.0;
if(doub == (int) ans){
Answer.setText(Integer.toString((int) ans));
} else{
Answer.setText(Double.toString(doub));
}
}
if(oper.equals("-")){
ans = num1 - num2;
doub = Math.round(ans * 100000.0) / 100000.0;
if(doub == (int) ans){
Answer.setText(Integer.toString((int) ans));
} else{
Answer.setText(Double.toString(doub));
}
}
if(oper.equals("/")){
ans = num1 / num2;
doub = Math.round(ans * 100000.0) / 100000.0;
if(doub == (int) ans){
Answer.setText(Integer.toString((int) ans));
} else{
Answer.setText(Double.toString(doub));
}
}
if(oper.equals("x")){
ans = num1 * num2;
doub = Math.round(ans * 100000.0) / 100000.0;
if(doub == (int) ans){
Answer.setText(Integer.toString((int) ans));
} else{
Answer.setText(Double.toString(doub));
}
}
答案 0 :(得分:5)
使用java-8,你可以通过引入一个接受数字和DoubleBinaryOperator
的新方法来使它变得非常有趣:
public void setAnswerText(double num1, double num2, DoubleBinaryOperator operator){
final double ans = operator.applyAsDouble(num1, num2);
final double doub = Math.round(ans * 100000.0) / 100000.0;
if(doub == (int) ans){
Answer.setText(Integer.toString((int) ans));
} else{
Answer.setText(Double.toString(doub));
}
}
并使用switch语句:
final double num1 = Double.parseDouble(FirstInput.getText());
final double num2 = Double.parseDouble(SecondInput.getText());
final String oper = (Operator.getText());
final DoubleBinaryOperator operator;
switch(oper){
case "+":
operator = (a, b) -> a+b;
break;
case "-":
operator = (a, b) -> a-b;
break;
case "/":
operator = (a, b) -> a/b;
break;
case "x":
operator = (a, b) -> a*b;
break;
default:
throw new UnsupportedOperationException();
}
setAnswerText(num1, num2, operator);
这样,你在switch-statment中找到了匹配的运算符,然后用数字和找到的运算符执行方法
答案 1 :(得分:0)
您可以将准备答案的部分提取到单独的方法中,这样您就不需要多次编写该代码,例如
private static void prepAnswer(double ans){
double doub = Math.round(ans * 100000.0) / 100000.0;
if(doub == (int) ans){
Answer.setText(Integer.toString((int) ans));
} else{
Answer.setText(Double.toString(doub));
}
}
此外,您可以使用switch-case语句。这看起来像这样:
String oper = (Operator.getText());
switch(oper) {
case "+": prepAnswer(num1 + num2); break;
case "-": prepAnswer(num1 - num2); break;
case "/": prepAnswer(num1 / num2); break;
case "x": prepAnswer(num1 * num2); break;
default: throw new UnsupportedOperationException();
}
答案 2 :(得分:0)
只是巩固
double num1, num2, ans, doub;
num1 = Double.parseDouble(FirstInput.getText());
num2 = Double.parseDouble(SecondInput.getText());
oper = (Operator.getText());
switch(oper){
case "+" : ans = num1 + num2; break;
case "-" : ans = num1 - num2; break;
case "/" : ans = num1 / num2; break;
case "*" : ans = num1 * num2; break;
}
setAnswer(ans);
}
/**
* @param ans
*/
private static void setAnswer(double ans) {
double doub;
doub = Math.round(ans * 100000.0) / 100000.0;
if (doub == (int) ans) {
Answer.setText(Integer.toString((int) ans));
} else {
Answer.setText(Double.toString(doub));
}
}
答案 3 :(得分:0)
我喜欢Lino的回答,使用DoubleBinaryOperator。(+ 1)。大多数ifs或switch可以被多态替换。没有ifs / switch的功能更易于阅读/测试和维护。受到谷歌谈话的影响很大https://www.youtube.com/watch?v=4F72VULWFvc 您也可以使用此ENUM版本。你需要JDK 8
First_Name
----------
Peter
Hans
Maria