JavaFX Calculator Doesn't Clear After Answer

烈酒焚心 提交于 2019-12-13 02:47:39

问题


Using Java, and JavaFX, I made a Scientific Calculator, but it has a problem with clearing after the answers on the "Scientific" buttons. It clears after the +/-/// equations, but not on like, square root, or tangent.
For example: Say I did 1 + 1, then pressed = and got 2. If I pressed another number, it would clear first, not just add onto the number like this: 25 But like this: 5 And if I pressed +/-/
// button, it would clear the text area, too of course, so that the user could put in another number.

Here's another example: Say I did 4 * 8, and got 32. If I pressed another number, it would clear first, not just add onto the number like this: 327 But like this: 7 And if I pressed +/-/*// button, it would clear the text area, too of course, so that the user could put in another number.

But here's a case with a problem: Say I did 16 , then pressed square root and got 4. If I pressed another number, it would just add onto the original number like this: 323 Not like this: 3 That's the problem with all the other scientific buttons, too.

Here's some code(my FXML controller class):

package calculator;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.input.KeyCode;
import javafx.stage.Stage;


public class ScienCalController implements Initializable {

    double data = 0d;
    int operation = -1;
    private boolean start = false;
    private boolean hypb = false;


    @FXML
    private Button hyp;

    @FXML
    private Label display;

    @FXML
    private Button one, two, three, four, five, six, seven, eight, nine, zero;

    @FXML
    private Button add, minus, multi, divide, equals, dot;

    @FXML
    private Button clear, backspace;

    @FXML
    private Button squared, cubed, squarert;

    @FXML
    private Button sin, cos, tan;


    @FXML
    void handleButtonAction(ActionEvent event) {

        if(start)
        {
            data = Double.parseDouble(display.getText());
            start = false;
        }

        if(event.getSource() == one)
        {
            display.setText(display.getText() + "1");
        }
        else if(event.getSource() == two)
        {
            display.setText(display.getText() + "2");
        }
        else if(event.getSource() == three)
        {
            display.setText(display.getText() + "3");
        }
        else if(event.getSource() == four)
        {
            display.setText(display.getText() + "4");
        }
        else if(event.getSource() == five)
        {
            display.setText(display.getText() + "5");
        }
        else if(event.getSource() == six)
        {
            display.setText(display.getText() + "6");
        }
        else if(event.getSource() == seven)
        {
            display.setText(display.getText() + "7");
        }
        else if(event.getSource() == eight)
        {
            display.setText(display.getText() + "8");
        }
        else if(event.getSource() == nine)
        {
            display.setText(display.getText() + "9");
        }
        else if(event.getSource() == zero)
        {
            display.setText(display.getText() + "0");
        }
        else if(event.getSource() == dot)
        {
            display.setText(display.getText() + ".");
            dot.setDisable(true);
        }
        else if(event.getSource() == clear)
        {
            display.setText("");
            dot.setDisable(false);
        } 
        else if(event.getSource() == add)
        {
            operation = -1; // need this line, or otherwise there will be bug
            operation = 1;
            data = Float.parseFloat(display.getText());
            display.setText("");
            dot.setDisable(false);
        }
        else if(event.getSource() == minus)
        {
            operation = -1; // need this line, or otherwise there will be bug
            operation = 2;
            data = Float.parseFloat(display.getText());
            display.setText("");
            dot.setDisable(false);
        }
        else if(event.getSource() == multi)
        {
            operation = -1;
            operation = 3;
            data = Float.parseFloat(display.getText());
            display.setText("");
            dot.setDisable(false);
        }
        else if(event.getSource() == divide)
        {
            operation = -1;
            operation = 4;
            data = Float.parseFloat(display.getText());
            display.setText("");
            dot.setDisable(false);
        }
        else if(event.getSource() == equals)
        {
            Double secondOperand = Double.parseDouble(display.getText());
            dot.setDisable(false);
            switch(operation)
            {
                case 1: //Addition
                    Double ans = data + secondOperand;
                    display.setText(String.valueOf(ans));
                    break;
                case 2: //Subtraction
                    ans = data - secondOperand;
                    display.setText(String.valueOf(ans));
                    break;
                case 3: //Multiplication
                    ans = data * secondOperand;
                    display.setText(String.valueOf(ans));
                    break;
                case 4: //Division
                    ans = 0d;
                    try{
                    ans = data / secondOperand;
                    }catch(Exception ex){display.setText("Error");}
                    display.setText(String.valueOf(ans));
                    break;

            }
            operation = -1;
            if(event.getSource() != divide && event.getSource() != add && event.getSource() != multi && event.getSource() != minus)
            {
                start = true;
                //it works here
            }
        }
        else if(event.getSource() == squared)
        {
            data = Double.parseDouble(display.getText());
            Double ans = data * data;
            display.setText(String.valueOf(ans));
            if(event.getSource() != divide && event.getSource() != add && event.getSource() != multi && event.getSource() != minus && event.getSource() != pow && event.getSource() != mod)
            {
                start = true;
                //but not here or the below buttons
            }
        }
        else if(event.getSource() == cubed)
        {
            data = Float.parseFloat(display.getText());
            Double ans = data * data * data;
            display.setText(String.valueOf(ans));
            dot.setDisable(false);
            if(event.getSource() != divide && event.getSource() != add && event.getSource() != multi && event.getSource() != minus && event.getSource() != pow && event.getSource() != mod)
            {
                start = true;
            }
        }
        else if(event.getSource() == squarert)
        {
            data = Float.parseFloat(display.getText());
            Double ans = Math.sqrt(data);
            display.setText(String.valueOf(ans));
            dot.setDisable(false);
            if(event.getSource() != divide && event.getSource() != add && event.getSource() != multi && event.getSource() != minus && event.getSource() != pow && event.getSource() != mod)
            {
                start = true;
            }
        }
        else if(event.getSource() == backspace)
        {
            if (display.getText().length()>0){
                StringBuffer sb = new StringBuffer(display.getText());
                sb = sb.deleteCharAt(display.getText().length()-1);
                display.setText(sb.toString());
            }
            dot.setDisable(false);
            if(event.getSource() != divide && event.getSource() != add && event.getSource() != multi && event.getSource() != minus && event.getSource() != pow && event.getSource() != mod)
            {
                start = true;
            }

        }
        else if(event.getSource() == sin)
        {
            if(hypb == true)
            {
                data = Float.parseFloat(display.getText());
                Double ans = Math.sinh(Math.toRadians(data));
                display.setText(String.valueOf(ans));
                dot.setDisable(false);
            }
            else if(hypb == false)
            {
                data = Float.parseFloat(display.getText());
                Double ans = Math.sin(Math.toRadians(data));
                display.setText(String.valueOf(ans));
                dot.setDisable(false);
            }
            dot.setDisable(false);
            if(event.getSource() != divide && event.getSource() != add && event.getSource() != multi && event.getSource() != minus && event.getSource() != pow && event.getSource() != mod)
            {
                start = true;
            }
        }
        else if(event.getSource() == cos)
        {
            data = Float.parseFloat(display.getText());
            Double ans = Math.cos(Math.toRadians(data));
            display.setText(String.valueOf(ans));
            dot.setDisable(false);  
            if(event.getSource() != divide && event.getSource() != add && event.getSource() != multi && event.getSource() != minus && event.getSource() != pow && event.getSource() != mod)
            {
                start = true;
            }

        }
        else if(event.getSource() == tan)
        {
            data = Float.parseFloat(display.getText());
            Double ans = Math.tan(Math.toRadians(data));
            display.setText(String.valueOf(ans));
            dot.setDisable(false);
            if(event.getSource() != divide && event.getSource() != add && event.getSource() != multi && event.getSource() != minus && event.getSource() != pow && event.getSource() != mod)
            {
                start = true;
            }
        }
        //there are more buttons, I'm just not going to show them all.

    }

来源:https://stackoverflow.com/questions/57100504/javafx-calculator-doesnt-clear-after-answer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!