用java写一个计算器,要求有用户使用界面,实现进制转换操作,有清除键和归零键,有异常处理
时间: 2024-06-09 18:07:59 浏览: 7
以下是一个简单的 Java 计算器示例,包括进制转换、清除键、归零键和异常处理。
```java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Calculator extends JFrame implements ActionListener {
private JTextField inputField; // 输入框
private JButton[] numberButtons; // 数字键
private JButton[] operatorButtons; // 操作符键
private JButton clearButton; // 清除键
private JButton resetButton; // 归零键
private JButton hexButton; // 十六进制转换键
private JButton binButton; // 二进制转换键
private JButton decButton; // 十进制转换键
private int currentOperator; // 当前操作符
private double result; // 计算结果
private int base; // 当前进制
public Calculator() {
setTitle("Calculator");
setSize(300, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new BorderLayout());
inputField = new JTextField(20);
add(inputField, BorderLayout.NORTH);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(5, 4));
String[] numberLabels = {"7", "8", "9", "4", "5", "6", "1", "2", "3", "0", ".", "+/-", "(", ")", "/", "*", "-", "+"};
numberButtons = new JButton[numberLabels.length];
for (int i = 0; i < numberLabels.length; i++) {
numberButtons[i] = new JButton(numberLabels[i]);
numberButtons[i].addActionListener(this);
buttonPanel.add(numberButtons[i]);
}
operatorButtons = new JButton[5];
operatorButtons[0] = new JButton("=");
operatorButtons[1] = new JButton("sin");
operatorButtons[2] = new JButton("cos");
operatorButtons[3] = new JButton("tan");
operatorButtons[4] = new JButton("^");
for (int i = 0; i < operatorButtons.length; i++) {
operatorButtons[i].addActionListener(this);
buttonPanel.add(operatorButtons[i]);
}
clearButton = new JButton("C");
clearButton.addActionListener(this);
buttonPanel.add(clearButton);
resetButton = new JButton("AC");
resetButton.addActionListener(this);
buttonPanel.add(resetButton);
hexButton = new JButton("HEX");
hexButton.addActionListener(this);
buttonPanel.add(hexButton);
binButton = new JButton("BIN");
binButton.addActionListener(this);
buttonPanel.add(binButton);
decButton = new JButton("DEC");
decButton.addActionListener(this);
buttonPanel.add(decButton);
add(buttonPanel, BorderLayout.CENTER);
setVisible(true);
base = 10; // 默认十进制
}
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if ("0123456789.".indexOf(command) >= 0) { // 数字键
inputField.setText(inputField.getText() + command);
} else if ("+-*/^".indexOf(command) >= 0) { // 操作符键
try {
double input = Double.parseDouble(inputField.getText());
if (currentOperator == 0) {
result = input;
} else if (currentOperator == 1) {
result += input;
} else if (currentOperator == 2) {
result -= input;
} else if (currentOperator == 3) {
result *= input;
} else if (currentOperator == 4) {
result /= input;
} else if (currentOperator == 5) {
result = Math.pow(result, input);
}
inputField.setText("");
currentOperator = "+-*/^".indexOf(command) + 1;
} catch (NumberFormatException ex) {
inputField.setText("Error");
}
} else if (command.equals("=")) { // 等号键
try {
double input = Double.parseDouble(inputField.getText());
if (currentOperator == 1) {
result += input;
} else if (currentOperator == 2) {
result -= input;
} else if (currentOperator == 3) {
result *= input;
} else if (currentOperator == 4) {
result /= input;
} else if (currentOperator == 5) {
result = Math.pow(result, input);
}
inputField.setText(Double.toString(result));
currentOperator = 0;
} catch (NumberFormatException ex) {
inputField.setText("Error");
}
} else if (command.equals("C")) { // 清除键
inputField.setText("");
} else if (command.equals("AC")) { // 归零键
inputField.setText("");
currentOperator = 0;
result = 0;
} else if (command.equals("HEX")) { // 十六进制转换键
try {
int input = Integer.parseInt(inputField.getText(), base);
inputField.setText(Integer.toHexString(input));
base = 16;
} catch (NumberFormatException ex) {
inputField.setText("Error");
}
} else if (command.equals("BIN")) { // 二进制转换键
try {
int input = Integer.parseInt(inputField.getText(), base);
inputField.setText(Integer.toBinaryString(input));
base = 2;
} catch (NumberFormatException ex) {
inputField.setText("Error");
}
} else if (command.equals("DEC")) { // 十进制转换键
try {
int input = Integer.parseInt(inputField.getText(), base);
inputField.setText(Integer.toString(input));
base = 10;
} catch (NumberFormatException ex) {
inputField.setText("Error");
}
} else if (command.equals("+/-")) { // 正负号键
try {
double input = Double.parseDouble(inputField.getText());
inputField.setText(Double.toString(-input));
} catch (NumberFormatException ex) {
inputField.setText("Error");
}
} else if (command.equals("(")) { // 左括号键
inputField.setText(inputField.getText() + "(");
} else if (command.equals(")")) { // 右括号键
inputField.setText(inputField.getText() + ")");
} else if (command.equals("sin")) { // sin键
try {
double input = Double.parseDouble(inputField.getText());
inputField.setText(Double.toString(Math.sin(input)));
} catch (NumberFormatException ex) {
inputField.setText("Error");
}
} else if (command.equals("cos")) { // cos键
try {
double input = Double.parseDouble(inputField.getText());
inputField.setText(Double.toString(Math.cos(input)));
} catch (NumberFormatException ex) {
inputField.setText("Error");
}
} else if (command.equals("tan")) { // tan键
try {
double input = Double.parseDouble(inputField.getText());
inputField.setText(Double.toString(Math.tan(input)));
} catch (NumberFormatException ex) {
inputField.setText("Error");
}
} else if (command.equals("^")) { // 幂运算键
try {
double input = Double.parseDouble(inputField.getText());
if (currentOperator == 0) {
result = input;
} else if (currentOperator == 1) {
result += input;
} else if (currentOperator == 2) {
result -= input;
} else if (currentOperator == 3) {
result *= input;
} else if (currentOperator == 4) {
result /= input;
} else if (currentOperator == 5) {
result = Math.pow(result, input);
}
inputField.setText("");
currentOperator = 5;
} catch (NumberFormatException ex) {
inputField.setText("Error");
}
}
}
public static void main(String[] args) {
new Calculator();
}
}
```
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)