赞
踩
主要用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列,行列间距为5
-
- addButton(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);
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。