21xrx.com
2024-12-22 23:20:23 Sunday
登录
文章检索 我的文章 写文章
【Java计算器代码】——详解如何用Java实现一个简单计算器
2023-06-16 12:25:35 深夜i     --     --
Java 计算器 代码案例

【Java计算器代码】——详解如何用Java实现一个简单计算器

计算器是我们日常生活和工作中常用的工具,如果能够用Java来实现一个简单的计算器是非常有用的。本文将详细介绍如何使用Java语言编写计算器代码,并提供实用的代码案例。

Java计算器代码实现步骤:

1. 设计计算器的界面

计算器的界面应该具备基本的数码键和运算符键,我们可以借助Swing或JavaFX等GUI框架来实现界面设计。

2. 实现计算器的逻辑

计算器的逻辑是指输入数字和运算符后进行相应的运算,并返回结果。我们需要编写相应的代码来实现这些功能。

3. 实现计算器的输入输出

计算器的输入应该包括数字和运算符,而输出则是对应的结果。我们需要在代码中定义相应的变量或数组来存储输入和输出的内容,并为其设置相应的处理方法。

4. 完善计算器代码

在实现计算器的过程中,我们需要考虑到一些异常情况和边界条件,例如除数为0、字符串输入等情况。我们需要在代码中添加相应的异常处理机制和边界判断。

示例代码:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class Calculator extends JFrame implements ActionListener {

  private JTextField display = new JTextField("0");

  private JButton clear = new JButton("C");

  private JButton[] numButtons = new JButton[10];

  private JButton[] operButtons = new JButton[4];

  private double op1, op2, result;

  private String operator;

  private boolean start = true;

  public Calculator() {

    setLayout(new BorderLayout());

    JPanel top = new JPanel(new FlowLayout(FlowLayout.CENTER));

    top.add(display);

    add(top, BorderLayout.NORTH);

    JPanel center = new JPanel(new GridLayout(4, 4, 5, 5));

    for (int i = 0; i < numButtons.length; i++) {

      numButtons[i] = new JButton(String.valueOf(i));

      numButtons[i].addActionListener(this);

    }

    operButtons[0] = new JButton("+");

    operButtons[1] = new JButton("-");

    operButtons[2] = new JButton("*");

    operButtons[3] = new JButton("/");

    for (int i = 0; i < operButtons.length; i++) {

      operButtons[i].addActionListener(this);

    }

    center.add(numButtons[1]);

    center.add(numButtons[2]);

    center.add(numButtons[3]);

    center.add(operButtons[0]);

    center.add(numButtons[4]);

    center.add(numButtons[5]);

    center.add(numButtons[6]);

    center.add(operButtons[1]);

    center.add(numButtons[7]);

    center.add(numButtons[8]);

    center.add(numButtons[9]);

    center.add(operButtons[2]);

    center.add(numButtons[0]);

    center.add(clear);

    center.add(operButtons[3]);

    add(center, BorderLayout.CENTER);

    clear.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent e) {

        display.setText("0");

      }

    });

  }

  public void actionPerformed(ActionEvent e) {

    String digit = e.getActionCommand();

    if (digit.equals("+")) {

      op2 = Double.parseDouble(display.getText());

      if (operator.equals("+")) {

        result = op1 + op2;

      } else if (operator.equals("-"))

        result = op1 - op2;

       else if (operator.equals("*")) {

        result = op1 * op2;

      } else if (operator.equals("/"))

        result = op1 / op2;

      display.setText(String.valueOf(result));

      operator = "+";

      start = true;

    } else if (digit.equals("-")) {

      op2 = Double.parseDouble(display.getText());

      if (operator.equals("+")) {

        result = op1 + op2;

      } else if (operator.equals("-"))

        result = op1 - op2;

       else if (operator.equals("*")) {

        result = op1 * op2;

      } else if (operator.equals("/"))

        result = op1 / op2;

      display.setText(String.valueOf(result));

      operator = "-";

      start = true;

    } else if (digit.equals("*")) {

      op2 = Double.parseDouble(display.getText());

      if (operator.equals("+")) {

        result = op1 + op2;

      } else if (operator.equals("-"))

        result = op1 - op2;

       else if (operator.equals("*")) {

        result = op1 * op2;

      } else if (operator.equals("/"))

        result = op1 / op2;

      display.setText(String.valueOf(result));

      operator = "*";

      start = true;

    } else if (digit.equals("/")) {

      op2 = Double.parseDouble(display.getText());

      if (operator.equals("+")) {

        result = op1 + op2;

      } else if (operator.equals("-"))

        result = op1 - op2;

       else if (operator.equals("*")) {

        result = op1 * op2;

      } else if (operator.equals("/"))

        result = op1 / op2;

      display.setText(String.valueOf(result));

      operator = "/";

      start = true;

    } else if (digit.equals("C")) {

      display.setText("0");

      operator = "";

      start = true;

    } else {

      if (start) {

        display.setText("");

        start = false;

      }

      display.setText(display.getText() + digit);

    }

    op1 = Double.parseDouble(display.getText());

  }

  public static void main(String[] args) {

    Calculator frame = new Calculator();

    frame.setTitle("Calculator");

    frame.setSize(250, 250);

    frame.setLocationRelativeTo(null);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.setVisible(true);

  }

}

  
  

评论区

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