如何在Java中检查用户是否输入了另一个数字:

时间:2015-09-04 08:19:55

标签: java javafx

我正在制作科学计算器并遇到了问题。

计算器的某些功能只需要1个数字(如:sin,log等) 和其他人需要2个数字(如:+, - 等)。

我想检查用户是否在文本字段中输入了另一个号码,因此它会执行 需要其他号码 的代码块,否则它将转到 不需要其他号码的功能。

这是我的Controller类(我正在使用JavaFX):

import javafx.event.ActionEvent;
import javafx.fxml.Initializable;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import java.net.URL;
import java.util.ResourceBundle;

public class Calculator_Controller implements Initializable {
    private String currentOperation;
    private double currentNumber;

    @FXML
    private TextField displayField = new TextField();

    @Override
    public void initialize(URL url, ResourceBundle resources) {
       //TODO
    }

    @FXML
    public void handleOperator(ActionEvent event) {

        double newNumber = new Double(displayField.getText());
        switch (currentOperation) {
            case "+":
                currentNumber += newNumber;
                break;
            case "-":
                currentNumber -= newNumber;
                break;
            case "÷":
                currentNumber /= newNumber;
                break;
            case "×":
                currentNumber *= newNumber;
                break;
            case "xⁿ":
                currentNumber = Math.pow(currentNumber, newNumber);
                break;
            default:
                break;
        }



            switch (currentOperation) {
                case "AC":
                    displayField.setText("");
                    break;
                case "n!":
                    currentNumber = factorial(currentNumber);
                    break;
                case "±":
                    currentNumber *= -1;
                    break;
                case "√":
                    currentNumber = Math.sqrt(currentNumber);
                    break;
                case "sin":
                    currentNumber = Math.sin(currentNumber);
                    break;
                case "cos":
                    currentNumber = Math.cos(currentNumber);
                    break;
                case "tan":
                    currentNumber = Math.tan(currentNumber);
                    break;
                case "x²":
                    currentNumber = Math.pow(currentNumber, 2);
                    break;
                case "10\uF006":
                    currentNumber = Math.pow(10, currentNumber);
                    break;
                case "log":
                    currentNumber = Math.log10(currentNumber);
                    break;
                default:
                    break;
            }

        displayField.setText("" + currentNumber);
    }

    @FXML
    public void handleOperation(ActionEvent event) {
        currentNumber = new Double(displayField.getText());
        displayField.setText("");
        currentOperation = ((Button) event.getSource()).getText();
    }

     public double factorial(double n) {
         if(n <= 1)
             return 1;
         else
             return n * factorial(n - 1);
     }
}

我删除了if-else,但你可以看到它应该在哪里。第一个开关用于if(),第二个开关用于else。

由于

P.S。我是否必须发布FXML文件和Main类,供大家理解?

1 个答案:

答案 0 :(得分:0)

我会试试这个:

@FXML
public void handleOperator(ActionEvent event) {

    double newNumber = new Double(displayField.getText());
    switch (currentOperation) {
        case "+":
            currentNumber += newNumber;
            break;
        case "-":
            currentNumber -= newNumber;
            break;
        case "÷":
            currentNumber /= newNumber;
            break;
        case "×":
            currentNumber *= newNumber;
            break;
        case "xⁿ":
            currentNumber = Math.pow(currentNumber, newNumber);
            break;
        default:
            break;
    }       
    displayField.setText("" + currentNumber);
}

@FXML
public void handleOperation(ActionEvent event) {
    currentNumber = new Double(displayField.getText());
    displayField.setText("");
    currentOperation = ((Button) event.getSource()).getText();
    switch (currentoperation) {
        case "AC":
            displayField.setText("");
            break;
        case "n!":
            currentNumber = factorial(currentNumber);
            break;
        case "±":
            currentNumber *= -1;
            break;
        case "√":
            currentNumber = Math.sqrt(currentNumber);
            break;
        case "sin":
            currentNumber = Math.sin(currentNumber);
            break;
        case "cos":
            currentNumber = Math.cos(currentNumber);
            break;
        case "tan":
            currentNumber = Math.tan(currentNumber);
            break;
        case "x²":
            currentNumber = Math.pow(currentNumber, 2);
            break;
        case "10\uF006":
            currentNumber = Math.pow(10, currentNumber);
            break;
        case "log":
            currentNumber = Math.log10(currentNumber);
            break;
        default:
            break;
    }
    displayField.setText("" + currentNumber);
}

正如您所看到的,我已将第二个switch移动到处理操作的方法,因此当您使用一元操作时它将立即执行。

编辑(解释)

当您将用一个操作数(一元)计算的第二个开关移动到处理运算符的方法时,它会在您按下运算符时计算结果。当按下另一个操作符(二进制,如+, - ,*,/,%等)时,切换不会计算任何内容,因为这种类型的操作符不存在。二进制运算符在您的方法中以您的写作方式计算。

注意:未经测试