计算器视图修改onEqualsButtonClicked修改

main
陈梦 1 month ago
parent 407dcadfeb
commit dca4d2f31e

@ -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();

@ -1,32 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Text?>
<AnchorPane prefHeight="501.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="CalculatorControl">
<children>
<Text layoutX="14.0" layoutY="30.0" strokeType="OUTSIDE" strokeWidth="0.0" text="计算器" />
<Button layoutX="300.0" layoutY="420.0" mnemonicParsing="false" prefHeight="50.0" prefWidth="50.0" text="=" onAction="#onEqualsButtonClicked" />
<Button layoutX="400.0" layoutY="420.0" mnemonicParsing="false" prefHeight="50.0" prefWidth="50.0" text="+" onAction="#onOperationButtonClicked" />
<Button layoutX="200.0" layoutY="420.0" mnemonicParsing="false" prefHeight="50.0" prefWidth="50.0" text="." onAction="#onNumberButtonClicked" />
<Button layoutX="100.0" layoutY="420.0" mnemonicParsing="false" prefHeight="50.0" prefWidth="50.0" text="0" onAction="#onNumberButtonClicked" />
<Button layoutX="100.0" layoutY="350.0" mnemonicParsing="false" prefHeight="50.0" prefWidth="50.0" text="1" onAction="#onNumberButtonClicked" />
<Button layoutX="100.0" layoutY="280.0" mnemonicParsing="false" prefHeight="50.0" prefWidth="50.0" text="4" onAction="#onNumberButtonClicked" />
<Button layoutX="100.0" layoutY="210.0" mnemonicParsing="false" prefHeight="50.0" prefWidth="50.0" text="7" onAction="#onNumberButtonClicked" />
<Button layoutX="200.0" layoutY="350.0" mnemonicParsing="false" prefHeight="50.0" prefWidth="50.0" text="2" onAction="#onNumberButtonClicked" />
<Button layoutX="200.0" layoutY="280.0" mnemonicParsing="false" prefHeight="50.0" prefWidth="50.0" text="5" onAction="#onNumberButtonClicked" />
<Button layoutX="200.0" layoutY="210.0" mnemonicParsing="false" prefHeight="50.0" prefWidth="50.0" text="8" onAction="#onNumberButtonClicked" />
<Button layoutX="300.0" layoutY="350.0" mnemonicParsing="false" prefHeight="50.0" prefWidth="50.0" text="3" onAction="#onNumberButtonClicked" />
<Button layoutX="300.0" layoutY="280.0" mnemonicParsing="false" prefHeight="50.0" prefWidth="50.0" text="6" onAction="#onNumberButtonClicked" />
<Button layoutX="400.0" layoutY="210.0" mnemonicParsing="false" prefHeight="50.0" prefWidth="50.0" text="/" onAction="#onOperationButtonClicked" />
<Button layoutX="400.0" layoutY="350.0" mnemonicParsing="false" prefHeight="50.0" prefWidth="50.0" text="-" onAction="#onOperationButtonClicked" />
<Button layoutX="400.0" layoutY="280.0" mnemonicParsing="false" prefHeight="50.0" prefWidth="50.0" text="*" onAction="#onOperationButtonClicked" />
<Button layoutX="100.0" layoutY="140.0" mnemonicParsing="false" prefHeight="50.0" prefWidth="50.0" text="\%" onAction="#onOperationButtonClicked" />
<Button layoutX="300.0" layoutY="210.0" mnemonicParsing="false" prefHeight="50.0" prefWidth="50.0" text="9" onAction="#onNumberButtonClicked" />
<Button layoutX="300.0" layoutY="140.0" mnemonicParsing="false" prefHeight="50.0" prefWidth="50.0" text="C" onAction="#onClearButtonClicked" />
<Button layoutX="200.0" layoutY="140.0" mnemonicParsing="false" prefHeight="50.0" prefWidth="50.0" text="CE" onAction="#onClearEntryButtonClicked" />
<Button layoutX="400.0" layoutY="140.0" mnemonicParsing="false" prefHeight="50.0" prefWidth="50.0" text="D" onAction="#onDeleteButtonClicked" />
<Button layoutX="300.0" layoutY="420.0" mnemonicParsing="false" onAction="#onEqualsButtonClicked" prefHeight="50.0" prefWidth="50.0" text="=" />
<Button layoutX="400.0" layoutY="420.0" mnemonicParsing="false" onAction="#onOperationButtonClicked" prefHeight="50.0" prefWidth="50.0" text="+" />
<Button layoutX="200.0" layoutY="420.0" mnemonicParsing="false" onAction="#onNumberButtonClicked" prefHeight="50.0" prefWidth="50.0" text="." />
<Button layoutX="100.0" layoutY="420.0" mnemonicParsing="false" onAction="#onNumberButtonClicked" prefHeight="50.0" prefWidth="50.0" text="0" />
<Button layoutX="100.0" layoutY="350.0" mnemonicParsing="false" onAction="#onNumberButtonClicked" prefHeight="50.0" prefWidth="50.0" text="1" />
<Button layoutX="100.0" layoutY="280.0" mnemonicParsing="false" onAction="#onNumberButtonClicked" prefHeight="50.0" prefWidth="50.0" text="4" />
<Button layoutX="100.0" layoutY="210.0" mnemonicParsing="false" onAction="#onNumberButtonClicked" prefHeight="50.0" prefWidth="50.0" text="7" />
<Button layoutX="200.0" layoutY="350.0" mnemonicParsing="false" onAction="#onNumberButtonClicked" prefHeight="50.0" prefWidth="50.0" text="2" />
<Button layoutX="200.0" layoutY="280.0" mnemonicParsing="false" onAction="#onNumberButtonClicked" prefHeight="50.0" prefWidth="50.0" text="5" />
<Button layoutX="200.0" layoutY="210.0" mnemonicParsing="false" onAction="#onNumberButtonClicked" prefHeight="50.0" prefWidth="50.0" text="8" />
<Button layoutX="300.0" layoutY="350.0" mnemonicParsing="false" onAction="#onNumberButtonClicked" prefHeight="50.0" prefWidth="50.0" text="3" />
<Button layoutX="300.0" layoutY="280.0" mnemonicParsing="false" onAction="#onNumberButtonClicked" prefHeight="50.0" prefWidth="50.0" text="6" />
<Button layoutX="400.0" layoutY="210.0" mnemonicParsing="false" onAction="#onOperationButtonClicked" prefHeight="50.0" prefWidth="50.0" text="/" />
<Button layoutX="400.0" layoutY="350.0" mnemonicParsing="false" onAction="#onOperationButtonClicked" prefHeight="50.0" prefWidth="50.0" text="-" />
<Button layoutX="400.0" layoutY="280.0" mnemonicParsing="false" onAction="#onOperationButtonClicked" prefHeight="50.0" prefWidth="50.0" text="*" />
<Button layoutX="100.0" layoutY="140.0" mnemonicParsing="false" onAction="#onOperationButtonClicked" prefHeight="50.0" prefWidth="50.0" text="\%" />
<Button layoutX="300.0" layoutY="210.0" mnemonicParsing="false" onAction="#onNumberButtonClicked" prefHeight="50.0" prefWidth="50.0" text="9" />
<Button layoutX="300.0" layoutY="140.0" mnemonicParsing="false" onAction="#onClearButtonClicked" prefHeight="50.0" prefWidth="50.0" text="C" />
<Button layoutX="200.0" layoutY="140.0" mnemonicParsing="false" onAction="#onClearEntryButtonClicked" prefHeight="50.0" prefWidth="50.0" text="CE" />
<Button layoutX="400.0" layoutY="140.0" mnemonicParsing="false" onAction="#onDeleteButtonClicked" prefHeight="50.0" prefWidth="50.0" text="D" />
<TextArea fx:id="display" layoutX="97.0" layoutY="33.0" prefHeight="81.0" prefWidth="363.0" />
</children>
</AnchorPane>
Loading…
Cancel
Save