主要用JAVA编写一个计算器,能实现加减乘除、开平方、求阶乘等计算功能,计算器有图形化界面,能对异常进行处理,并且检验输入的合法性,对不合法的输入给出具体的提示信息
代码如下
import java.awt.*;import java.awt.event.*;import javax.swing.*;public class CalculatorFrame extends JFrame implements ActionListener {private JTextField resultDisplay; //结果显示文本框private double num1; //第一个数字private double num2; //第二个数字private char operator = 'C'; //操作符,默认为清屏public CalculatorFrame() {super("计算器");JPanel mainPanel = new JPanel(); //主面板mainPanel.setLayout(new BorderLayout());resultDisplay = new JTextField(); //结果显示文本框resultDisplay.setEditable(false); //不能编辑mainPanel.add(resultDisplay, BorderLayout.NORTH); //添加到面板上方JPanel buttonPanel = new JPanel(new GridLayout(5, 4, 5, 5)); //按钮面板,4行4列,行列间距为5addButton(buttonPanel, "7"); addButton(buttonPanel, "8");addButton(buttonPanel, "9");addButton(buttonPanel, "÷"); //添加按钮:“7”、“8”、“9”、“÷”addButton(buttonPanel, "4");addButton(buttonPanel, "5");addButton(buttonPanel, "6");addButton(buttonPanel, "×"); //添加按钮:“4”、“5”、“6”、“×”addButton(buttonPanel, "1");addButton(buttonPanel, "2");addButton(buttonPanel, "3");addButton(buttonPanel, "-"); //添加按钮:“1”、“2”、“3”、“-”addButton(buttonPanel, "0");addButton(buttonPanel, ".");addButton(buttonPanel, "C");addButton(buttonPanel, "+"); //添加按钮:“0”、“.”、“C”、“+”addButton(buttonPanel, "x²");addButton(buttonPanel, "√");addButton(buttonPanel, "x!");addButton(buttonPanel, "="); //添加按钮:“x²”、“√”、“x!”、“=” mainPanel.add(buttonPanel, BorderLayout.CENTER); //添加按钮面板,位于主面板的中央JButton equalsButton = new JButton("="); //等于号按钮equalsButton.addActionListener(this); //添加事件响应器mainPanel.add(equalsButton, BorderLayout.SOUTH); //添加到主面板下方add(mainPanel); //将主面板添加到窗口中}private void addButton(Container panel, String label) {JButton button = new JButton(label); //新建按钮button.addActionListener(this); //添加事件响应器panel.add(button); //添加到按钮面板中}// 响应用户操作事件public void actionPerformed(ActionEvent evt) {String text = evt.getActionCommand(); //获取用户操作if (text.equals("C")) { //清屏resultDisplay.setText("");num1 = 0;num2 = 0;operator = 'C';return;}if (text.equals("x²")) { //平方num1 = Double.parseDouble(resultDisplay.getText()); //获取当前数字resultDisplay.setText(String.valueOf(num1 * num1)); //计算平方并显示operator = 'C';return;}if (text.equals("√")) { //开方num1 = Double.parseDouble(resultDisplay.getText()); //获取当前数字if (num1 < 0) { //负数不能开方resultDisplay.setText("不能对负数求平方根");return;}resultDisplay.setText(String.valueOf(Math.sqrt(num1))); //计算开方并显示operator = 'C';return;}if (text.equals("x!")) { //阶乘num1 = Double.parseDouble(resultDisplay.getText()); //获取当前数字resultDisplay.setText(String.valueOf(factorial(num1))); //计算阶乘并显示operator = 'C';return;}if (text.equals("+") || text.equals("-") || text.equals("×") ||text.equals("÷")) { //基本运算num1 = Double.parseDouble(resultDisplay.getText()); //获取第一个数字operator = text.charAt(0); //获取操作符resultDisplay.setText(""); //清空结果文本框return;}if (text.equals("=")) { //计算结果num2 = Double.parseDouble(resultDisplay.getText()); //获取第二个数字switch (operator) { //选择合适的操作符进行计算case '+':resultDisplay.setText(String.valueOf(num1 + num2));break;case '-':resultDisplay.setText(String.valueOf(num1 - num2));break;case '×':resultDisplay.setText(String.valueOf(num1 * num2));break;case '÷':if (num2 == 0) { //除数不能为零resultDisplay.setText("除数不能为零");return;}resultDisplay.setText(String.valueOf(num1 / num2));break;}operator = 'C'; //清除操作符return;}resultDisplay.setText(resultDisplay.getText() + text); //输出数字或小数点}// 计算阶乘public static double factorial(double num) {if (num == 0)return 1;double result = 1;for (int i = 1; i <= num; i++) {result *= i;}return result;}public static void main(String[] args) {CalculatorFrame calculator = new CalculatorFrame();calculator.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);calculator.setSize(300, 350);calculator.setVisible(true);}}