parent
5b2bc1aa01
commit
d16335da8e
@ -1,71 +1,122 @@
|
|||||||
import javafx.event.ActionEvent;
|
|
||||||
import javafx.fxml.FXML;
|
import javafx.fxml.FXML;
|
||||||
import javafx.scene.control.TextField;
|
import javafx.scene.control.TextField;
|
||||||
|
import javafx.scene.control.Button;
|
||||||
|
import javafx.event.ActionEvent;
|
||||||
|
|
||||||
public class CalculatorController {
|
public class CalculatorController {
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
private TextField inputField;
|
private TextField inputField;
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
private TextField inputField1;
|
private TextField inputField1; // 用于显示计算记录
|
||||||
|
|
||||||
@FXML
|
private double firstNumber = 0;
|
||||||
private TextField inputField2;
|
private String operator = "";
|
||||||
|
private boolean isOperationClicked = false;
|
||||||
|
|
||||||
@FXML
|
@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
|
@FXML
|
||||||
void handleClearAction(ActionEvent event) {
|
protected void handleAddAction(ActionEvent event) {
|
||||||
|
performOperation("+");
|
||||||
}
|
}
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
void handleClearsAction(ActionEvent event) {
|
protected void handleSubtractAction(ActionEvent event) {
|
||||||
|
performOperation("-");
|
||||||
}
|
}
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
void handleDivideAction(ActionEvent event) {
|
protected void handleMultiplyAction(ActionEvent event) {
|
||||||
|
performOperation("*");
|
||||||
}
|
}
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
void handleEqualAction(ActionEvent event) {
|
protected void handleDivideAction(ActionEvent event) {
|
||||||
|
performOperation("/");
|
||||||
}
|
}
|
||||||
|
|
||||||
@FXML
|
@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
|
@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
|
@FXML
|
||||||
void handleNumberAction(ActionEvent event) {
|
protected void handleClearAction(ActionEvent event) {
|
||||||
|
inputField.clear();
|
||||||
|
firstNumber = 0;
|
||||||
|
operator = "";
|
||||||
|
isOperationClicked = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
void handlePointAction(ActionEvent event) {
|
protected void handleClearsAction(ActionEvent event) {
|
||||||
|
inputField1.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
void handleRemoveAction(ActionEvent event) {
|
protected void handlePointAction(ActionEvent event) {
|
||||||
|
if (!inputField.getText().contains(".")) {
|
||||||
|
inputField.setText(inputField.getText() + ".");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@FXML
|
@FXML
|
||||||
void handleSubtractAction(ActionEvent event) {
|
protected void handleRemoveAction(ActionEvent event) {
|
||||||
|
inputField1.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Binary file not shown.
@ -1,2 +1,4 @@
|
|||||||
# Calculator
|
# Calculator 计算器简单运算
|
||||||
|
- `要求:`
|
||||||
|
- 实现计算器加减乘除取余等的实现
|
||||||
|
|
||||||
|
Loading…
Reference in new issue