每次我尝试在文本字段中打印一个负数时,它总是打印0。我想不出原因,因为每当我使用调试器时,它都会显示sum存储一个负数。只要其他运算符不是负数,它们都将返回正确的数字。我还在程序中使用javafx。
谢谢!
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));
}
}
答案 0 :(得分:0)
-的值为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);
}
}