首页 > 解决方案 > 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));

    }




}

标签: java

解决方案


- 的值为 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);
        }

    }

推荐阅读