java科学计算器代码
时间: 2023-12-17 22:03:48 浏览: 124
以下是一个Java科学计算器的代码示例,它包括了加减乘除、小数点、正负号、三角函数、指数运算、阶乘、求倒数、退格和清零等功能:
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ScientificCalculator extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private JTextField display;
private JButton[] buttons;
private String[] buttonLabels = { "7", "8", "9", "/", "4", "5", "6", "*", "1", "2", "3", "-", "0", ".", "+", "=",
"sin", "cos", "tan", "x^y", "x!", "1/x", "C", "Back" };
private double num1, num2, result;
private String operator;
private boolean isOperatorClicked = false;
public ScientificCalculator() {
setTitle("Scientific Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 300);
setLocationRelativeTo(null);
display = new JTextField(20); display.setEditable(false);
display.setHorizontalAlignment(JTextField.RIGHT);
add(display, BorderLayout.NORTH);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(6, 4));
buttons = new JButton[buttonLabels.length];
for (int i = 0; i < buttonLabels.length; i++) {
buttons[i] = new JButton(buttonLabels[i]);
buttons[i].addActionListener(this);
buttonPanel.add(buttons[i]);
}
add(buttonPanel, BorderLayout.CENTER);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String label = e.getActionCommand();
if (label.equals("C")) {
display.setText("");
} else if (label.equals("Back")) {
String text = display.getText();
if (text.length() > 0) {
display.setText(text.substring(0, text.length() - 1));
}
} else if (label.equals("+") || label.equals("-") || label.equals("*") || label.equals("/")) {
num1 = Double.parseDouble(display.getText());
operator = label;
isOperatorClicked = true;
} else if (label.equals("=")) {
if (isOperatorClicked) {
num2 = Double.parseDouble(display.getText());
if (operator.equals("+")) {
result = num1 + num2;
} else if (operator.equals("-")) {
result = num1 - num2;
} else if (operator.equals("*")) {
result = num1 * num2;
} else if (operator.equals("/")) {
result = num1 / num2;
}
display.setText(Double.toString(result));
isOperatorClicked = false;
}
} else if (label.equals(".")) {
String text = display.getText();
if (text.indexOf(".") == -1) {
display.setText(text + ".");
}
} else if (label.equals("sin")) {
double value = Double.parseDouble(display.getText());
display.setText(Double.toString(Math.sin(value)));
} else if (label.equals("cos")) {
double value = Double.parseDouble(display.getText());
display.setText(Double.toString(Math.cos(value)));
} else if (label.equals("tan")) {
double value = Double.parseDouble(display.getText());
display.setText(Double.toString(Math.tan(value)));
} else if (label.equals("x^y")) {
num1 = Double.parseDouble(display.getText());
operator = "^";
isOperatorClicked = true;
} else if (label.equals("x!")) {
int value = Integer.parseInt(display.getText());
int factorial = 1;
for (int i = 1; i <= value; i++) {
factorial *= i;
}
display.setText(Integer.toString(factorial));
} else if (label.equals("1/x")) {
double value = Double.parseDouble(display.getText());
display.setText(Double.toString(1 / value));
} else {
display.setText(display.getText() + label);
}
}
public static void main(String[] args) {
new ScientificCalculator();
}
}
```
阅读全文