21xrx.com
2024-11-22 08:04:57 Friday
登录
文章检索 我的文章 写文章
Java实现计算器:从入门到实践
2023-06-15 16:36:37 深夜i     --     --
Java 计算器 GUI组件

计算器系统是程序员学习编程的必修课程之一,而用Java编写计算器系统则是学习Java编程的好方法。下面将为大家介绍如何用Java实现一个简单的计算器系统。

先来看看Java实现计算器系统的代码:


import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class Calculator extends JFrame{

  private JButton b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, bPoint;

  private JButton bPlus, bMinus, bMultiply, bDivide, bEquals, bClear;

  private double num1, num2, result;

  private String operator;

  public Calculator(){

    super("Java Calculator");

    setLayout(new GridLayout(4,4));

    b0 = new JButton("0");

    b0.addActionListener(new ButtonListener());

    add(b0);

    

    b1 = new JButton("1");

    b1.addActionListener(new ButtonListener());

    add(b1);

    

    b2 = new JButton("2");

    b2.addActionListener(new ButtonListener());

    add(b2);

    

    b3 = new JButton("3");

    b3.addActionListener(new ButtonListener());

    add(b3);

    

    b4 = new JButton("4");

    b4.addActionListener(new ButtonListener());

    add(b4);

    

    b5 = new JButton("5");

    b5.addActionListener(new ButtonListener());

    add(b5);

    

    b6 = new JButton("6");

    b6.addActionListener(new ButtonListener());

    add(b6);

    

    b7 = new JButton("7");

    b7.addActionListener(new ButtonListener());

    add(b7);

    

    b8 = new JButton("8");

    b8.addActionListener(new ButtonListener());

    add(b8);

    

    b9 = new JButton("9");

    b9.addActionListener(new ButtonListener());

    add(b9);

    

    bPlus = new JButton("+");

    bPlus.addActionListener(new ButtonListener());

    add(bPlus);

    

    bMinus = new JButton("-");

    bMinus.addActionListener(new ButtonListener());

    add(bMinus);

    

    bMultiply = new JButton("*");

    bMultiply.addActionListener(new ButtonListener());

    add(bMultiply);

    

    bDivide = new JButton("/");

    bDivide.addActionListener(new ButtonListener());

    add(bDivide);

    

    bPoint = new JButton(".");

    bPoint.addActionListener(new ButtonListener());

    add(bPoint);

    

    bEquals = new JButton("=");

    bEquals.addActionListener(new ButtonListener());

    add(bEquals);

    

    bClear = new JButton("C");

    bClear.addActionListener(new ButtonListener());

    add(bClear);

    setSize(250, 250);

    setLocationRelativeTo(null);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    setVisible(true);

  }

  private class ButtonListener implements ActionListener {

    public void actionPerformed(ActionEvent e) {

      if (e.getSource() == b0) {

        String input = JOptionPane.showInputDialog("Enter number");

        num1 = Double.parseDouble(input);

      } else if (e.getSource() == b1) {

        String input = JOptionPane.showInputDialog("Enter number");

        num1 = Double.parseDouble(input);

      } else if (e.getSource() == b2) {

        String input = JOptionPane.showInputDialog("Enter number");

        num1 = Double.parseDouble(input);

      } else if (e.getSource() == b3) {

        String input = JOptionPane.showInputDialog("Enter number");

        num1 = Double.parseDouble(input);

      } else if (e.getSource() == b4) {

        String input = JOptionPane.showInputDialog("Enter number");

        num1 = Double.parseDouble(input);

      } else if (e.getSource() == b5) {

        String input = JOptionPane.showInputDialog("Enter number");

        num1 = Double.parseDouble(input);

      } else if (e.getSource() == b6) {

        String input = JOptionPane.showInputDialog("Enter number");

        num1 = Double.parseDouble(input);

      } else if (e.getSource() == b7) {

        String input = JOptionPane.showInputDialog("Enter number");

        num1 = Double.parseDouble(input);

      } else if (e.getSource() == b8) {

        String input = JOptionPane.showInputDialog("Enter number");

        num1 = Double.parseDouble(input);

      } else if (e.getSource() == b9) {

        String input = JOptionPane.showInputDialog("Enter number");

        num1 = Double.parseDouble(input);

      } else if (e.getSource() == bPlus)

        operator = ".";

       else if (e.getSource() == bMinus)

        operator = "-";

       else if (e.getSource() == bMultiply) {

        operator = "*";

      } else if (e.getSource() == bDivide)

        operator = "/";

       else if (e.getSource() == bPoint) {

        operator = ".";

      } else if (e.getSource() == bEquals) {

        String input = JOptionPane.showInputDialog("Enter number");

        num2 = Double.parseDouble(input);

        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;

        

        JOptionPane.showMessageDialog(null, "Result is: " + result);

      } else if (e.getSource() == bClear)

        operator = "";

        num1 = 0;

        num2 = 0;

        result = 0;

      

    }

  }

  public static void main(String args[]){

    Calculator calc = new Calculator();

  }

}

以上就是Java实现计算器系统的完整代码,使用时可以运行主方法。在这个计算器系统中,我们使用了Java GUI组件,并通过ActionListener接口来监听它们。用户可以通过输入框输入数字,并通过按钮输入运算符号,然后点击"="按钮计算结果。如果用户要清空历史输入,可以点击"C"按钮。

Java实现计算器系统的例子,对初学者来说是一个非常好的代码案例。如果你正在学习Java编程,这个例子将帮助你更好地理解Java语言的特性和应用。但是,这个例子仅仅只是一个非常简单的Demo,有很多地方需要改进。如果你想扩展这个程序,可以使用更复杂的算法或增加额外的功能,比如一些高级的指数和三角函数运算。

总之,Java实现计算器系统是一个有趣实践,可以帮助你更好地理解Java编程。如果你正在学习Java,这个例子可以加速你的学习进程,帮助你掌握Java编程的基本特性。

  
  
下一篇: 题目及解析

评论区

{{item['qq_nickname']}}
()
回复
回复