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