我已经使用 JFrame 用 java 制作了一个计算器,除了除法之外一切正常。这是计算答案并显示它的代码位:
public void actionPerformed(ActionEvent e) {
try {
num1 = Double.parseDouble(resultl.getText());
num2 = Double.parseDouble(resultl.getText());
if (oper.equals("+")) {
answer = num1 + num2;
resultm.setText("" + answer);
resultl.setText("");
resultr.setText("");
oper = "";
currentNuml = "";
currentNumr = "";
} else if (oper.equals("-")) {
answer = num1 - num2;
resultm.setText("" + answer);
resultl.setText("");
resultr.setText("");
oper = "";
currentNuml = "";
currentNumr = "";
} else if (oper.equals("*")) {
answer = num1 * num2;
resultm.setText("" + answer);
resultl.setText("");
resultr.setText("");
oper = "";
currentNuml = "";
currentNumr = "";
} else if (oper.equals("/")) {
answer = num1 / num2;
resultm.setText("" + answer);
resultl.setText("");
resultr.setText("");
oper = "";
currentNuml = "";
currentNumr = "";
}
} catch (NumberFormatException ex) {
resultl.setText("Unexpected Error, try again.");
}
}
我粘贴了生成比较答案的整个内容,但实际上除法位是问题所在。如果我对除法做任何事情,它总是说答案是 1.0 - 任何帮助将不胜感激。