使用java编辑一个计算器,使它 实现基本数学运算、函数等功能:加、减、乘、除、阶乘、正弦、余弦和指数运算。
时间: 2023-07-10 12:37:06 浏览: 93
好的,我可以提供一些思路和代码示例,帮助你完成这个任务。
首先,你需要设计一个用户界面,让用户可以输入数字和运算符,然后显示计算结果。你可以使用Java Swing或JavaFX来创建GUI。这里我以JavaFX为例,示范如何创建一个简单的计算器界面:
```java
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class Calculator extends Application {
private TextField num1Field;
private TextField num2Field;
private TextField resultField;
@Override
public void start(Stage primaryStage) throws Exception {
// 创建布局
GridPane root = new GridPane();
root.setAlignment(Pos.CENTER);
root.setHgap(10);
root.setVgap(10);
root.setPadding(new Insets(25, 25, 25, 25));
// 添加控件
Label num1Label = new Label("Number 1:");
root.add(num1Label, 0, 0);
num1Field = new TextField();
root.add(num1Field, 1, 0);
Label num2Label = new Label("Number 2:");
root.add(num2Label, 0, 1);
num2Field = new TextField();
root.add(num2Field, 1, 1);
Label resultLabel = new Label("Result:");
root.add(resultLabel, 0, 2);
resultField = new TextField();
resultField.setEditable(false);
root.add(resultField, 1, 2);
Button addButton = new Button("+");
addButton.setOnAction(e -> {
double num1 = Double.parseDouble(num1Field.getText());
double num2 = Double.parseDouble(num2Field.getText());
double result = num1 + num2;
resultField.setText(String.valueOf(result));
});
root.add(addButton, 2, 0);
Button subtractButton = new Button("-");
subtractButton.setOnAction(e -> {
double num1 = Double.parseDouble(num1Field.getText());
double num2 = Double.parseDouble(num2Field.getText());
double result = num1 - num2;
resultField.setText(String.valueOf(result));
});
root.add(subtractButton, 2, 1);
Button multiplyButton = new Button("*");
multiplyButton.setOnAction(e -> {
double num1 = Double.parseDouble(num1Field.getText());
double num2 = Double.parseDouble(num2Field.getText());
double result = num1 * num2;
resultField.setText(String.valueOf(result));
});
root.add(multiplyButton, 3, 0);
Button divideButton = new Button("/");
divideButton.setOnAction(e -> {
double num1 = Double.parseDouble(num1Field.getText());
double num2 = Double.parseDouble(num2Field.getText());
double result = num1 / num2;
resultField.setText(String.valueOf(result));
});
root.add(divideButton, 3, 1);
Button factorialButton = new Button("!");
factorialButton.setOnAction(e -> {
int num = Integer.parseInt(num1Field.getText());
int result = 1;
for (int i = 2; i <= num; i++) {
result *= i;
}
resultField.setText(String.valueOf(result));
});
root.add(factorialButton, 2, 2);
Button sinButton = new Button("sin");
sinButton.setOnAction(e -> {
double num = Double.parseDouble(num1Field.getText());
double result = Math.sin(num);
resultField.setText(String.valueOf(result));
});
root.add(sinButton, 3, 2);
Button cosButton = new Button("cos");
cosButton.setOnAction(e -> {
double num = Double.parseDouble(num1Field.getText());
double result = Math.cos(num);
resultField.setText(String.valueOf(result));
});
root.add(cosButton, 4, 2);
Button expButton = new Button("exp");
expButton.setOnAction(e -> {
double num = Double.parseDouble(num1Field.getText());
double result = Math.exp(num);
resultField.setText(String.valueOf(result));
});
root.add(expButton, 4, 1);
// 显示窗口
Scene scene = new Scene(root, 400, 200);
primaryStage.setScene(scene);
primaryStage.setTitle("Calculator");
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
```
在这个示例中,我们创建了一个包含两个输入框和一个输出框的界面,用户可以输入两个数字,然后通过点击按钮进行计算。我们实现了加、减、乘、除、阶乘、正弦、余弦和指数运算,这些运算都是通过Java内置的Math类来实现的。
需要注意的是,这个示例中没有进行输入校验,如果用户输入的不是数字,程序会崩溃。你需要对用户的输入进行校验,以防止程序出错。
希望这个示例能够帮到你,如果还有什么问题,可以继续问我。
阅读全文