diff --git a/Calculator.java b/Calculator.java index 2cc6251..96ae16f 100644 --- a/Calculator.java +++ b/Calculator.java @@ -7,7 +7,7 @@ public class Calculator extends Application { @Override public void start(Stage primaryStage) throws Exception { - FXMLLoader loader = new FXMLLoader(getClass().getResource("Calculator.fxml")); + FXMLLoader loader = new FXMLLoader(getClass().getResource("CalculatorView.fxml")); Scene scene = new Scene(loader.load()); primaryStage.setTitle("简易计算器"); primaryStage.setScene(scene); diff --git a/CalculatorController.java b/CalculatorController.java index a367dcf..0df6d32 100644 --- a/CalculatorController.java +++ b/CalculatorController.java @@ -1,71 +1,122 @@ -import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.scene.control.TextField; +import javafx.scene.control.Button; +import javafx.event.ActionEvent; public class CalculatorController { @FXML private TextField inputField; - @FXML - private TextField inputField1; + private TextField inputField1; // 用于显示计算记录 - @FXML - private TextField inputField2; + private double firstNumber = 0; + private String operator = ""; + private boolean isOperationClicked = false; @FXML - void handleAddAction(ActionEvent event) { - + protected void handleNumberAction(ActionEvent event) { + Button button = (Button) event.getSource(); + String number = button.getText(); + if (isOperationClicked) { + inputField.clear(); + isOperationClicked = false; + } + inputField.setText(inputField.getText() + number); } @FXML - void handleClearAction(ActionEvent event) { - + protected void handleAddAction(ActionEvent event) { + performOperation("+"); } @FXML - void handleClearsAction(ActionEvent event) { - + protected void handleSubtractAction(ActionEvent event) { + performOperation("-"); } @FXML - void handleDivideAction(ActionEvent event) { - + protected void handleMultiplyAction(ActionEvent event) { + performOperation("*"); } @FXML - void handleEqualAction(ActionEvent event) { - + protected void handleDivideAction(ActionEvent event) { + performOperation("/"); } @FXML - void handleModulusAction(ActionEvent event) { + protected void handleModulusAction(ActionEvent event) { + performOperation("%"); + } + private void performOperation(String op) { + firstNumber = Double.parseDouble(inputField.getText()); + operator = op; + isOperationClicked = true; } @FXML - void handleMultiplyAction(ActionEvent event) { + protected void handleEqualAction(ActionEvent event) { + double secondNumber = Double.parseDouble(inputField.getText()); + double result = 0; + + switch (operator) { + case "+": + result = firstNumber + secondNumber; + break; + case "-": + result = firstNumber - secondNumber; + break; + case "*": + result = firstNumber * secondNumber; + break; + case "/": + if (secondNumber != 0) { + result = firstNumber / secondNumber; + } else { + inputField.setText("Error"); + return; + } + break; + case "%": + result = firstNumber % secondNumber; + break; + default: + return; + } + + inputField.setText(String.valueOf(result)); + recordOperation(firstNumber, secondNumber, result); + } + private void recordOperation(double a, double b, double result) { + String operation = a + " " + operator + " " + b + " = " + result; + inputField1.setText(inputField1.getText() + "\n" + operation); } @FXML - void handleNumberAction(ActionEvent event) { - + protected void handleClearAction(ActionEvent event) { + inputField.clear(); + firstNumber = 0; + operator = ""; + isOperationClicked = false; } @FXML - void handlePointAction(ActionEvent event) { - + protected void handleClearsAction(ActionEvent event) { + inputField1.clear(); } @FXML - void handleRemoveAction(ActionEvent event) { - + protected void handlePointAction(ActionEvent event) { + if (!inputField.getText().contains(".")) { + inputField.setText(inputField.getText() + "."); + } } @FXML - void handleSubtractAction(ActionEvent event) { - + protected void handleRemoveAction(ActionEvent event) { + inputField1.clear(); } - } diff --git a/out/production/Calculator/Calculator.class b/out/production/Calculator/Calculator.class index 2e6a5f0..98f3658 100644 Binary files a/out/production/Calculator/Calculator.class and b/out/production/Calculator/Calculator.class differ diff --git a/out/production/Calculator/README.md b/out/production/Calculator/README.md index dc7115b..200ea41 100644 --- a/out/production/Calculator/README.md +++ b/out/production/Calculator/README.md @@ -1,2 +1,4 @@ -# Calculator +# Calculator 计算器简单运算 +- `要求:` +- 实现计算器加减乘除取余等的实现