java - Why isn't my calculator not returning negative values?
问题描述
Every time I try to print a negative number to the textfield it always prints 0. I can't think of a reason because whenever I use the debugger it shows that sum stores a negative number. All the other operators return the right number as long as they are not negative. Also im using javafx for the program.
Thanks!
public class Calculator extends Application implements EventHandler<ActionEvent> {
Button[] numpadButtons = new Button[10];
Button[] operatorButtons = new Button[6];
String[] numButtonNames = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "0"};
String[] operatorButtonNames = {"+", "-", "x", "/", "=", "C"};
TextField label;
String input = "";
int sum = 0;
int num1 = 0;
int num2 = 0;
char operator = ' ';
int numOperators = 0;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
GridPane gPane = new GridPane();
BorderPane bPane = new BorderPane();
label = new TextField();
label.setEditable(false);
Scene scene = new Scene(bPane, 300, 400);
primaryStage.setScene(scene);
primaryStage.show();
}
@Override
public void handle(ActionEvent event) {
try {
if (event.getSource() == numpadButtons[0]) {
input += "1";
label.setText(input);
} else if (event.getSource() == numpadButtons[1]) {
input += "2";
label.setText(input);
} else if (event.getSource() == numpadButtons[2]) {
input += "3";
label.setText(input);
} else if (event.getSource() == numpadButtons[3]) {
input += "4";
label.setText(input);
} else if (event.getSource() == numpadButtons[4]) {
input += "5";
label.setText(input);
} else if (event.getSource() == numpadButtons[5]) {
input += "6";
label.setText(input);
} else if (event.getSource() == numpadButtons[6]) {
input += "7";
label.setText(input);
} else if (event.getSource() == numpadButtons[7]) {
input += "8";
label.setText(input);
} else if (event.getSource() == numpadButtons[8]) {
input += "9";
label.setText(input);
} else if (event.getSource() == numpadButtons[9]) {
input += "0";
label.setText(input);
} else if (event.getSource() == operatorButtons[4]) {
if (numOperators == 1) {
parseInput();
calculate();
label.setText(Integer.toString(sum));
clearNums();
sum = 0;
} else if (numOperators == 0) {
throw new ArithmeticException("No operator detected");
} else {
throw new ArithmeticException("Unknown error");
}
} else if (event.getSource() == operatorButtons[0]) {
input += "+";
++numOperators;
operator = '+';
label.setText(input);
} else if (event.getSource() == operatorButtons[1]) {
input += "-";
++numOperators;
operator = '-';
label.setText(input);
} else if (event.getSource() == operatorButtons[2]) {
input += "x";
++numOperators;
operator = 'x';
label.setText(input);
} else if (event.getSource() == operatorButtons[3]) {
input += "/";
++numOperators;
operator = '/';
label.setText(input);
} else if (event.getSource() == operatorButtons[5]) {
clearNums();
clearScreen();
sum = 0;
}
} catch (ArithmeticException e) {
clearNums();
clearScreen();
sum = 0;
label.setText(e.getMessage());
}
}
public void calculate() throws ArithmeticException {
if (operator == 47) {//if operator is a forward slash
if (num2 != 0) {
sum = (num1 / num2);
} else {
throw new ArithmeticException("Arithmetic error");
}
} else if (operator == 61) { // -
sum = (num1 - num2);
} else if (operator == 43) { //+
sum = (num1 + num2);
} else if (operator == 120) {
sum = (num1 * num2);
}
}
public void parseInput() {
int indexOfOperator = input.indexOf(operator);
int inputLength = input.length();
num1 = Integer.parseInt(input.substring(0, indexOfOperator));
num2 = Integer.parseInt(input.substring(indexOfOperator + 1, inputLength));
}
}
解决方案
- 的值为 45。试试这个。
public void calculate() throws ArithmeticException {
System.out.println((int)operator);
if (operator == 47) {// if operator is a forward slash
if (num2 != 0) {
sum = (num1 / num2);
} else {
throw new ArithmeticException("Arithmetic error");
}
} else if (operator == 45) { // -
sum = (num1 - num2);
} else if (operator == 43) { // +
sum = (num1 + num2);
} else if (operator == 120) {
sum = (num1 * num2);
}
}
推荐阅读
- ios - NSURLSession:流媒体延迟峰值
- python - 在列表中查找最接近均值的值
- sql - 与 SQL Server 中的日期或日期时间列进行比较
- jquery - 调用多次调用 API 的函数
- php - Symfony 3 - 无法实现 FOSUser 安全登录,因为我也使用 CAS 身份验证
- javascript - 来自 javascript 的强制事件
- java - 从 Spring Boot App 调用 Weblogic Server 上的 SOAP 服务时发生 ClassCastException
- javascript - 如何在我的代码中修复此错误 [object Object]
- angular - 为什么代码段会造成内存泄漏?
- php - Codeigniter:错误 - call_user_func_array() 期望参数 1 是有效的回调,类 'Error' 没有方法 'index'