计算器视图修改onEqualsButtonClicked修改

main
陈梦 1 month ago
parent 407dcadfeb
commit dca4d2f31e

@ -42,26 +42,68 @@ public class CalculatorControl {
String[] parts = displayText.split(" "); String[] parts = displayText.split(" ");
try { try {
secondNumber = Double.parseDouble(parts[parts.length - 1]); secondNumber = Double.parseDouble(parts[parts.length - 1]);
switch (operation) { // 检查两个数字是否都是整数
case "+": if ((firstNumber % 1 == 0) && (secondNumber % 1 == 0)) {
display.setText(firstNumber + " + " + secondNumber + " = " + (firstNumber + secondNumber)); int intFirstNumber = (int) firstNumber;
break; int intSecondNumber = (int) secondNumber;
case "-": switch (operation) {
display.setText(firstNumber + " - " + secondNumber + " = " + (firstNumber - secondNumber)); case "+":
break; int result = intFirstNumber + intSecondNumber;
case "*": display.setText(String.format("%d + %d = %d", intFirstNumber, intSecondNumber, result));
display.setText(firstNumber + " * " + secondNumber + " = " + (firstNumber * secondNumber)); break;
break; case "-":
case "/": result = intFirstNumber - intSecondNumber;
if (secondNumber == 0) { display.setText(String.format("%d - %d = %d", intFirstNumber, intSecondNumber, result));
display.setText("Error"); break;
} else { case "*":
display.setText(firstNumber + " / " + secondNumber + " = " + (firstNumber / secondNumber)); result = intFirstNumber * intSecondNumber;
} display.setText(String.format("%d * %d = %d", intFirstNumber, intSecondNumber, result));
break; break;
case "%": case "/":
display.setText(firstNumber + " % " + secondNumber + " = " + (firstNumber % secondNumber)); if (intSecondNumber == 0) {
break; 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; awaitingSecondNumber = false;
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
@ -70,6 +112,7 @@ public class CalculatorControl {
} }
} }
@FXML @FXML
void onNumberButtonClicked(ActionEvent event) { void onNumberButtonClicked(ActionEvent event) {
Button button = (Button) event.getSource(); Button button = (Button) event.getSource();

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