main
parent
e45c82da4b
commit
511d30bedd
@ -1,66 +1,100 @@
|
||||
import java.util.Scanner;
|
||||
import javafx.application.Application;
|
||||
import javafx.geometry.Insets;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.scene.control.Button;
|
||||
import javafx.scene.control.TextField;
|
||||
import javafx.scene.layout.GridPane;
|
||||
import javafx.scene.layout.VBox;
|
||||
import javafx.stage.Stage;
|
||||
|
||||
public class Calculator {
|
||||
// 加法
|
||||
public static double add(double num1, double num2) {
|
||||
return num1 + num2;
|
||||
}
|
||||
public class Calculator extends Application {
|
||||
private TextField display;
|
||||
private double result = 0;
|
||||
private String lastCommand = "=";
|
||||
private boolean start = true;
|
||||
|
||||
// 减法
|
||||
public static double subtract(double num1, double num2) {
|
||||
return num1 - num2;
|
||||
}
|
||||
@Override
|
||||
public void start(Stage primaryStage) {
|
||||
// 创建显示区域
|
||||
display = new TextField("0");
|
||||
display.setEditable(false);
|
||||
display.setPrefColumnCount(15);
|
||||
|
||||
// 乘法
|
||||
public static double multiply(double num1, double num2) {
|
||||
return num1 * num2;
|
||||
}
|
||||
// 创建按钮
|
||||
GridPane grid = new GridPane();
|
||||
grid.setPadding(new Insets(10));
|
||||
grid.setHgap(10);
|
||||
grid.setVgap(10);
|
||||
|
||||
// 除法
|
||||
public static double divide(double num1, double num2) {
|
||||
if (num2 == 0) {
|
||||
throw new IllegalArgumentException("除数不能为零");
|
||||
// 按钮布局
|
||||
String[][] buttonLayout = {
|
||||
{"7", "8", "9", "/"},
|
||||
{"4", "5", "6", "*"},
|
||||
{"1", "2", "3", "-"},
|
||||
{"0", ".", "=", "+"}
|
||||
};
|
||||
|
||||
for (int i = 0; i < 4; i++) {
|
||||
for (int j = 0; j < 4; j++) {
|
||||
Button button = new Button(buttonLayout[i][j]);
|
||||
button.setOnAction(e -> processButtonPress(button.getText()));
|
||||
grid.add(button, j, i);
|
||||
}
|
||||
}
|
||||
return num1 / num2;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
// 创建主布局
|
||||
VBox root = new VBox();
|
||||
root.setPadding(new Insets(10));
|
||||
root.setSpacing(10);
|
||||
root.getChildren().addAll(display, grid);
|
||||
|
||||
System.out.println("请输入第一个数字:");
|
||||
double num1 = scanner.nextDouble();
|
||||
// 创建场景
|
||||
Scene scene = new Scene(root, 300, 300);
|
||||
|
||||
System.out.println("请输入第二个数字:");
|
||||
double num2 = scanner.nextDouble();
|
||||
// 设置舞台
|
||||
primaryStage.setTitle("简易计算器");
|
||||
primaryStage.setScene(scene);
|
||||
primaryStage.show();
|
||||
}
|
||||
|
||||
System.out.println("请选择操作 (+, -, *, /):");
|
||||
String operator = scanner.next();
|
||||
private void processButtonPress(String command) {
|
||||
if (command.matches("[0-9]")) {
|
||||
if (start) {
|
||||
display.setText(command);
|
||||
start = false;
|
||||
} else {
|
||||
display.setText(display.getText() + command);
|
||||
}
|
||||
} else if (command.equals(".")) {
|
||||
if (start) {
|
||||
display.setText("0.");
|
||||
start = false;
|
||||
} else if (!display.getText().contains(".")) {
|
||||
display.setText(display.getText() + ".");
|
||||
}
|
||||
} else {
|
||||
calculate(Double.parseDouble(display.getText()));
|
||||
lastCommand = command;
|
||||
start = true;
|
||||
}
|
||||
}
|
||||
|
||||
double result;
|
||||
switch (operator) {
|
||||
case "+":
|
||||
result = add(num1, num2);
|
||||
break;
|
||||
case "-":
|
||||
result = subtract(num1, num2);
|
||||
break;
|
||||
case "*":
|
||||
result = multiply(num1, num2);
|
||||
break;
|
||||
case "/":
|
||||
try {
|
||||
result = divide(num1, num2);
|
||||
} catch (IllegalArgumentException e) {
|
||||
System.out.println(e.getMessage());
|
||||
return;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
System.out.println("无效的操作符");
|
||||
return;
|
||||
private void calculate(double n) {
|
||||
if (lastCommand.equals("+")) {
|
||||
result += n;
|
||||
} else if (lastCommand.equals("-")) {
|
||||
result -= n;
|
||||
} else if (lastCommand.equals("*")) {
|
||||
result *= n;
|
||||
} else if (lastCommand.equals("/")) {
|
||||
result /= n;
|
||||
} else {
|
||||
result = n;
|
||||
}
|
||||
display.setText(String.format("%.0f", result));
|
||||
}
|
||||
|
||||
System.out.println("结果: " + num1 + " " + operator + " " + num2 + " = " + result);
|
||||
scanner.close();
|
||||
public static void main(String[] args) {
|
||||
launch(args);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in new issue