diff --git a/CalculatorControl.java b/CalculatorControl.java index 2a35cd9..9b047c3 100644 --- a/CalculatorControl.java +++ b/CalculatorControl.java @@ -42,26 +42,68 @@ public class CalculatorControl { String[] parts = displayText.split(" "); try { secondNumber = Double.parseDouble(parts[parts.length - 1]); - switch (operation) { - case "+": - display.setText(firstNumber + " + " + secondNumber + " = " + (firstNumber + secondNumber)); - break; - case "-": - display.setText(firstNumber + " - " + secondNumber + " = " + (firstNumber - secondNumber)); - break; - case "*": - display.setText(firstNumber + " * " + secondNumber + " = " + (firstNumber * secondNumber)); - break; - case "/": - if (secondNumber == 0) { - display.setText("Error"); - } else { - display.setText(firstNumber + " / " + secondNumber + " = " + (firstNumber / secondNumber)); - } - break; - case "%": - display.setText(firstNumber + " % " + secondNumber + " = " + (firstNumber % secondNumber)); - break; + // 检查两个数字是否都是整数 + if ((firstNumber % 1 == 0) && (secondNumber % 1 == 0)) { + int intFirstNumber = (int) firstNumber; + int intSecondNumber = (int) secondNumber; + switch (operation) { + case "+": + int result = intFirstNumber + intSecondNumber; + display.setText(String.format("%d + %d = %d", intFirstNumber, intSecondNumber, result)); + break; + case "-": + result = intFirstNumber - intSecondNumber; + display.setText(String.format("%d - %d = %d", intFirstNumber, intSecondNumber, result)); + break; + case "*": + result = intFirstNumber * intSecondNumber; + display.setText(String.format("%d * %d = %d", intFirstNumber, intSecondNumber, result)); + break; + case "/": + if (intSecondNumber == 0) { + display.setText("Error"); + } else { + result = intFirstNumber / intSecondNumber; + display.setText(String.format("%d / %d = %d", intFirstNumber, intSecondNumber, result)); + } + break; + case "%": + result = intFirstNumber % intSecondNumber; + display.setText(String.format("%d %% %d = %d", intFirstNumber, intSecondNumber, result)); + break; + } + } else if ((firstNumber % 1 != 0) || (secondNumber % 1 != 0)) { + double doubleFirstNumber = firstNumber; + double doubleSecondNumber = secondNumber; + switch (operation) { + case "+": + double result = doubleFirstNumber + doubleSecondNumber; + display.setText(String.format("%.2f + %.2f = %.2f", doubleFirstNumber, doubleSecondNumber, result)); + break; + case "-": + result = doubleFirstNumber - doubleSecondNumber; + display.setText(String.format("%.2f - %.2f = %.2f", doubleFirstNumber, doubleSecondNumber, result)); + break; + case "*": + result = doubleFirstNumber * doubleSecondNumber; + display.setText(String.format("%.2f * %.2f = %.2f", doubleFirstNumber, doubleSecondNumber, result)); + break; + case "/": + if (doubleSecondNumber == 0) { + display.setText("Error"); + } else { + result = doubleFirstNumber / doubleSecondNumber; + display.setText(String.format("%.2f / %.2f = %.2f", doubleFirstNumber, doubleSecondNumber, result)); + } + break; + case "%": + double intPart = Math.floor(doubleFirstNumber); + double remainder = doubleFirstNumber - intPart; + display.setText(String.format("%.2f %% %.2f = %.2f", doubleFirstNumber, doubleSecondNumber, remainder)); + break; + } + } else { + display.setText("Non-integer input"); } awaitingSecondNumber = false; } catch (NumberFormatException e) { @@ -70,6 +112,7 @@ public class CalculatorControl { } } + @FXML void onNumberButtonClicked(ActionEvent event) { Button button = (Button) event.getSource(); diff --git a/CalculatorView.fxml b/CalculatorView.fxml index 3864900..6dde34a 100644 --- a/CalculatorView.fxml +++ b/CalculatorView.fxml @@ -1,32 +1,31 @@ + - - -