回答后无法清除JavaFX计算器

时间:2019-07-18 18:16:33

标签: java javafx calculator

使用Java和JavaFX,我制作了一个科学计算器,但是在清除“科学”按钮上的答案后,它有一个问题。它会在+ /-/ //等式之后清除,但不会在像,平方根或切线上清除。
例如: 假设我做了1 + 1,然后按=并得到2。如果我按另一个数字,它将首先清除,而不仅仅是像这样添加数字:25 但是像这样:5如果我按下+ /-/
//按钮,它当然也会清除文本区域,以便用户可以输入另一个数字。

这是另一个例子: 假设我做了4 * 8,得到32。如果我按下另一个数字,它将首先清除,而不是像这样添加到数字上:327 但是像这样:7如果我按下+ /-/ * //按钮,它当然也会清除文本区域,以便用户可以输入另一个数字。

但是这里有一个问题: 假设我做了16次,然后按了平方根并得到4。如果我按了另一个数字,它会像这样添加到原始数字中:323 不是这样的:3这也是所有其他科学按钮的问题。

这是一些代码(我的FXML控制器类):

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.

    }

0 个答案:

没有答案