diff --git a/Main.java b/Main.java index c6d43f7..3347310 100644 --- a/Main.java +++ b/Main.java @@ -1,107 +1,120 @@ -import java.util.Scanner; - -public class Main { - - public static void main(String[] args) { - Scanner scanner = new Scanner(System.in); - - System.out.println("欢迎使用简单计算器!"); - System.out.println("请选择操作:"); - System.out.println("1. 加法"); - System.out.println("2. 减法"); - System.out.println("3. 乘法"); - System.out.println("4. 除法"); - System.out.println("5. 取余"); - System.out.print("输入你的选择(1/2/3/4/5): "); - - int choice = scanner.nextInt(); - - switch (choice) { - case 1: - performAddition(scanner); - break; - case 2: - performSubtraction(scanner); - break; - case 3: - performMultiplication(scanner); - break; - case 4: - performDivision(scanner); - break; - case 5: - performModulus(scanner); - break; - default: - System.out.println("无效选择,请重试。"); +import javafx.application.Application; +import javafx.beans.binding.Bindings; +import javafx.beans.property.*; +import javafx.geometry.Pos; +import javafx.scene.Scene; +import javafx.scene.control.*; +import javafx.scene.layout.*; +import javafx.stage.Stage; + +public class Main extends Application { + private TextField display = new TextField(); + private Button[] numberButtons = new Button[10]; + private Button addButton, subtractButton, multiplyButton, divideButton, modulusButton, equalsButton, clearButton; + private Label statusLabel = new Label(""); + + private SimpleDoubleProperty firstOperand = new SimpleDoubleProperty(0); + private SimpleDoubleProperty secondOperand = new SimpleDoubleProperty(0); + private String operator = ""; + + @Override + public void start(Stage primaryStage) { + GridPane grid = new GridPane(); + grid.setAlignment(Pos.CENTER); + grid.setHgap(10); + grid.setVgap(10); + + display.setEditable(false); + grid.add(display, 0, 0, 4, 1); + + for (int i = 0; i < 10; i++) { + numberButtons[i] = new Button(String.valueOf(i)); + grid.add(numberButtons[i], (i % 3), (i / 3) + 1); } - scanner.close(); - } - - private static void performAddition(Scanner scanner) { - System.out.print("输入第一个数字: "); - double num1 = scanner.nextDouble(); - - System.out.print("输入第二个数字: "); - double num2 = scanner.nextDouble(); - - double result = num1 + num2; - - System.out.println("结果: " + result); - } - - private static void performSubtraction(Scanner scanner) { - System.out.print("输入第一个数字: "); - double num1 = scanner.nextDouble(); - - System.out.print("输入第二个数字: "); - double num2 = scanner.nextDouble(); - - double result = num1 - num2; + addButton = new Button("+"); + subtractButton = new Button("-"); + multiplyButton = new Button("*"); + divideButton = new Button("/"); + modulusButton = new Button("%"); + equalsButton = new Button("="); + clearButton = new Button("C"); + + grid.add(addButton, 3, 1); + grid.add(subtractButton, 3, 2); + grid.add(multiplyButton, 3, 3); + grid.add(divideButton, 3, 4); + grid.add(modulusButton, 3, 5); + grid.add(equalsButton, 3, 6); + grid.add(clearButton, 3, 7); + + for (int i = 0; i < 10; i++) { + int finalI = i; + numberButtons[i].setOnAction(e -> display.appendText(String.valueOf(finalI))); + } - System.out.println("结果: " + result); + addButton.setOnAction(e -> setOperator("+")); + subtractButton.setOnAction(e -> setOperator("-")); + multiplyButton.setOnAction(e -> setOperator("*")); + divideButton.setOnAction(e -> setOperator("/")); + modulusButton.setOnAction(e -> setOperator("%")); + equalsButton.setOnAction(e -> calculate()); + clearButton.setOnAction(e -> clear()); + + Scene scene = new Scene(grid, 300, 400); + primaryStage.setTitle("Calculator"); + primaryStage.setScene(scene); + primaryStage.show(); } - private static void performMultiplication(Scanner scanner) { - System.out.print("输入第一个数字: "); - double num1 = scanner.nextDouble(); - - System.out.print("输入第二个数字: "); - double num2 = scanner.nextDouble(); - - double result = num1 * num2; - - System.out.println("结果: " + result); + private void setOperator(String op) { + if (!operator.isEmpty()) { + calculate(); + } + operator = op; + firstOperand.setValue(Double.parseDouble(display.getText())); + display.clear(); } - private static void performDivision(Scanner scanner) { - System.out.print("输入第一个数字: "); - double num1 = scanner.nextDouble(); - - System.out.print("输入第二个数字: "); - double num2 = scanner.nextDouble(); - - if (num2 == 0) { - System.out.println("错误: 除数不能为零。"); - } else { - double result = num1 / num2; - System.out.println("结果: " + result); + private void calculate() { + if (!operator.isEmpty()) { + secondOperand.setValue(Double.parseDouble(display.getText())); + double result = 0; + switch (operator) { + case "+": + result = firstOperand.get() + secondOperand.get(); + break; + case "-": + result = firstOperand.get() - secondOperand.get(); + break; + case "*": + result = firstOperand.get() * secondOperand.get(); + break; + case "/": + if (secondOperand.get() != 0) { + result = firstOperand.get() / secondOperand.get(); + } else { + statusLabel.setText("Cannot divide by zero"); + return; + } + break; + case "%": + result = firstOperand.get() % secondOperand.get(); + break; + } + display.setText(String.format("%.2f", result)); + operator = ""; } } - private static void performModulus(Scanner scanner) { - System.out.print("输入第一个数字: "); - int num1 = scanner.nextInt(); - - System.out.print("输入第二个数字: "); - int num2 = scanner.nextInt(); + private void clear() { + display.clear(); + firstOperand.setValue(0); + secondOperand.setValue(0); + operator = ""; + } - if (num2 == 0) { - System.out.println("错误: 除数不能为零。"); - } else { - int result = num1 % num2; - System.out.println("结果: " + result); - } + public static void main(String[] args) { + launch(args); } } diff --git a/out/production/jeidui1/Main.class b/out/production/jeidui1/Main.class index 2da2f9b..0ec60dd 100644 Binary files a/out/production/jeidui1/Main.class and b/out/production/jeidui1/Main.class differ