Java 计算器帮助

标签 java calculator

这是我在这里发表的第一篇文章,我希望能在我的学校项目上得到一些帮助。该项目是用 Java 制作一个计算器,我已经做到了,它在大多数情况下都运行良好,我遇到的唯一问题是当我得到答案时 - 让我们说 5+5=10并显示 10,当我想输入另一个数字时,假设我想输入 8*10,为了做到这一点,我写了 8,而不是删除之前的答案 10,而是写 8 而不是那个数字将在 10 之后写 8,所以它将是 108。我想要的是在给出答案后输入新数字后删除之前的答案。我希望我解释得很好,这是我的代码,我希望得到一些帮助,因为我尝试的一切都没有奏效。提前致谢。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Calculator_UI implements ActionListener {

    JFrame frame = new JFrame("Calculator");
    JPanel panel = new JPanel();
    JTextArea text = new JTextArea(1, 20);
    JButton but1 = new JButton("1");
    JButton but2 = new JButton("2");
    JButton but3 = new JButton("3");
    JButton but4 = new JButton("4");
    JButton but5 = new JButton("5");
    JButton but6 = new JButton("6");
    JButton but7 = new JButton("7");
    JButton but8 = new JButton("8");
    JButton but9 = new JButton("9");
    JButton but0 = new JButton("0");
    JButton butadd = new JButton("+");
    JButton butsub = new JButton("-");
    JButton butmulti = new JButton("*");
    JButton butdiv = new JButton("/");
    JButton buteq = new JButton("=");
    JButton butclear = new JButton("C");
    Double number1, number2, result;
    int addc = 0, subc = 0, multic = 0, divc = 0;

    public void ui() {
        frame.setVisible(true);
        frame.setSize(230, 200);
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.add(panel);

        panel.add(text);
        panel.add(but1);
        panel.add(but2);
        panel.add(but3);
        panel.add(but4);
        panel.add(but5);
        panel.add(but6);
        panel.add(but7);
        panel.add(but8);
        panel.add(but9);
        panel.add(but0);

        panel.add(butadd);
        panel.add(butsub);
        panel.add(butmulti);
        panel.add(butdiv);
        panel.add(buteq);
        panel.add(butclear);

        but1.addActionListener(this);
        but1.setBackground(Color.cyan);
        but2.addActionListener(this);
        but2.setBackground(Color.cyan);
        but3.addActionListener(this);
        but3.setBackground(Color.cyan);
        but4.addActionListener(this);
        but4.setBackground(Color.cyan);
        but5.addActionListener(this);
        but5.setBackground(Color.cyan);
        but6.addActionListener(this);
        but6.setBackground(Color.cyan);
        but7.addActionListener(this);
        but7.setBackground(Color.cyan);
        but8.addActionListener(this);
        but8.setBackground(Color.cyan);
        but9.addActionListener(this);
        but9.setBackground(Color.cyan);
        but0.addActionListener(this);
        but0.setBackground(Color.cyan);

        butadd.addActionListener(this);
        butadd.setBackground(Color.cyan);
        butsub.addActionListener(this);
        butsub.setBackground(Color.cyan);
        butmulti.addActionListener(this);
        butmulti.setBackground(Color.cyan);
        butdiv.addActionListener(this);
        butdiv.setBackground(Color.cyan);
        buteq.addActionListener(this);
        buteq.setBackground(Color.cyan);
        butclear.addActionListener(this);
        butclear.setBackground(Color.cyan);
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        Object source = e.getSource();

        if (source == butclear) {
            number1 = 0.0;
            number2 = 0.0;
            text.setText("");
        }

        if (source == but1) {
            text.append("1");
        }
        if (source == but2) {
            text.append("2");
        }
        if (source == but3) {
            text.append("3");
        }
        if (source == but4) {
            text.append("4");
        }
        if (source == but5) {
            text.append("5");
        }
        if (source == but6) {
            text.append("6");
        }
        if (source == but7) {
            text.append("7");
        }
        if (source == but8) {
            text.append("8");
        }
        if (source == but9) {
            text.append("9");
        }
        if (source == but0) {
            text.append("0");
        }
        if (source == butadd) {
            number1 = number_reader();
            text.setText("");
            addc = 1;
            subc = 0;
            multic = 0;
            divc = 0;
        }
        if (source == butsub) {
            number1 = number_reader();
            text.setText("");
            addc = 0;
            subc = 1;
            multic = 0;
            divc = 0;
        }
        if (source == butmulti) {
            number1 = number_reader();
            text.setText("");
            addc = 0;
            subc = 0;
            multic = 1;
            divc = 0;
        }
        if (source == butdiv) {
            number1 = number_reader();
            text.setText("");
            addc = 0;
            subc = 0;
            multic = 0;
            divc = 1;
        }

        if (source == buteq) {
            number2 = number_reader();
            if (addc == 1) {
                result = number1 + number2;
                text.setText(Double.toString(result));
            }
            if (subc == 1) {
                result = number1 - number2;
                text.setText(Double.toString(result));

            }
            if (multic == 1) {
                result = number1 * number2;
                text.setText(Double.toString(result));

            }
            if (divc == 1) {
                result = number1 / number2;
                text.setText(Double.toString(result));
            }
        }
    }

    public double number_reader() {
        Double num1;
        String s;
        s = text.getText();
        num1 = Double.valueOf(s);

        return num1;
    }
}

最佳答案

当用户按下 = 并打印结果时,您应该设置一个标志为 true,以便下次按下数字按钮时,首先删除结果。

事实上,您甚至应该将整个逻辑转换为 state diagram ,因为如果,例如,用户按下 + 两次,您可能应该显示一条错误消息或忽略第二个 +,而不是读取当前数字。每个状态都应该定义按下任何按钮时会发生什么,以及下一个状态取决于按下哪个按钮。

由于 GUI 可以比真正的计算器更智能,每个状态还可以定义启用哪些按钮以及禁用哪些按钮。

关于Java 计算器帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20935133/

相关文章:

java包声明

java - 逆波兰给出错误答案

Java Switch语句: creating a calculator error

java - 将旧版 e.printStractTrace() 记录到 log4j

基于商品价格和总价的 Javascript 计算器

c - 尝试从 getchar() 获取变量以在简单的计算程序中保持其值

android - 计算器问题 - Android编程

java - 为什么java中引用类型可以与对象类型不同

java - 是否可以对java方法进行注释以在方法中的主代码之前和之后添加代码?

java - c 中的 JSTL 请求属性 :if