开发者社区> 问答> 正文

我在计算器上找不到代码0 * 2 = 2或0/2 = 2的解决方案

我在calculate()中有问题。当我按0-2或0 * 2或0/2时打开计算器时,总是得出我在零之前输入的数字。如果我不这样做,否则我打开计算器时不编码第一个数字。在编程方面,请帮我一个新手。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Toolkit;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.SwingConstants;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JLabel;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;


public class Calculator{

    private JFrame frame;
    private JButton btnX;
    private JButton button_1, button;
    private JButton button_2;
    private JLabel label;
    private JLabel label2;
    private JButton button_3;
    private JButton button_4;
    private JButton button_5;
    private JButton button_6;
    private JButton button_7;
    private JButton button_8;
    private JButton button_9, button_15;
    private JButton button_11, button_14;
    private JButton button_12, button_16;
    private JButton button_13, button_17;
    private JButton button_22;
    private Double result = 0.0; 
    private Double inverse;
    private Double squareRoot;
    private Double square;
    private Double percent;
    private boolean newin = true;
    private String arg = "";

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Calculator window = new Calculator();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public Calculator() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        ImageIcon image = new ImageIcon("D:/Programming/MyApps/src/1440353685_MB__calculator.png");
        frame = new JFrame();
        KeyListener l = new KeyListener() {
            @Override
            public void keyPressed(KeyEvent e) {
                switch (e.getKeyCode()) {
                case KeyEvent.VK_0 :
                case KeyEvent.VK_NUMPAD0 : button_9.doClick();
                break;
                case KeyEvent.VK_NUMPAD1 :
                case KeyEvent.VK_1 : button.doClick();
                break;
                case KeyEvent.VK_2 :
                case KeyEvent.VK_NUMPAD2 : button_1.doClick();
                break;
                case KeyEvent.VK_3 :
                case KeyEvent.VK_NUMPAD3 : button_2.doClick();
                break;
                case KeyEvent.VK_4 :
                case KeyEvent.VK_NUMPAD4 : button_3.doClick();
                break;
                case KeyEvent.VK_5 :
                case KeyEvent.VK_NUMPAD5 : button_4.doClick();
                break;
                case KeyEvent.VK_6 :
                case KeyEvent.VK_NUMPAD6 : button_5.doClick();
                break;
                case KeyEvent.VK_7 :
                case KeyEvent.VK_NUMPAD7 : button_6.doClick();
                break;
                case KeyEvent.VK_8 : if (e.isShiftDown()) button_14.doClick(); 
                else button_7.doClick();
                break;
                case KeyEvent.VK_NUMPAD8 : button_7.doClick();
                break;
                case KeyEvent.VK_9 :
                case KeyEvent.VK_NUMPAD9 : button_8.doClick();
                break;
                case KeyEvent.VK_PERIOD :
                case KeyEvent.VK_DECIMAL : button_13.doClick();
                break;
                case KeyEvent.VK_EQUALS : if(e.isShiftDown()) button_12.doClick();
                else button_17.doClick();
                break;
                case KeyEvent.VK_ENTER : button_17.doClick();
                break;
                case KeyEvent.VK_MINUS :
                case KeyEvent.VK_SUBTRACT : button_11.doClick();
                break;
                case KeyEvent.VK_C : button_16.doClick();
                break;
                case KeyEvent.VK_ADD : button_12.doClick();
                break;
                case KeyEvent.VK_MULTIPLY : button_14.doClick();
                break;
                case KeyEvent.VK_DIVIDE :
                case KeyEvent.VK_SLASH : button_15.doClick();
                break;
                case KeyEvent.VK_BACK_SPACE : btnX.doClick();
                break;
                case KeyEvent.VK_AMPERSAND : button_22.doClick();
                }

            }

            @Override
            public void keyReleased(KeyEvent e) {

            }

            @Override
            public void keyTyped(KeyEvent e) {
                // TODO Auto-generated method stub

            }
        };
        frame.setResizable(false);
        frame.setBounds(100, 100, 289, 452);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("Calculator");
        frame.addKeyListener(l);
        frame.setIconImage(image.getImage());
        frame.getContentPane().setLayout(null);

        Border border = LineBorder.createGrayLineBorder();

        button = new JButton("1");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                if (label.getText().length() == 22) {
                    Toolkit.getDefaultToolkit().beep();
                    return;
                }
                if (label.getText().equals("0") || newin) label.setText("");
                label.setText(label.getText() + "1");
                newin = false;
            }
        });
        button.setBounds(10, 68, 47, 34);
        button.setFocusable(false);
        button.setContentAreaFilled(false);
        frame.getContentPane().add(button);

        btnX = new JButton("<X");
        btnX.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (label.getText().length() > 0 && !label.getText().equals("0")) {
                    label.setText(label.getText().substring(0, label.getText().length()-1));
                }
                if (label.getText().length() == 0) label.setText("0");
            }
        });
        btnX.setBounds(224, 68, 49, 34);
        btnX.setFocusable(false);
        btnX.setToolTipText("Press \"Backspace\" instead");
        btnX.setContentAreaFilled(false);
        frame.getContentPane().add(btnX);

        button_1 = new JButton("2");
        button_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (label.getText().length() == 22) {
                    Toolkit.getDefaultToolkit().beep();
                    return;
                }
                if (label.getText().equals("0") || newin) label.setText("");
                label.setText(label.getText() + "2");
                newin = false;
            }
        });
        button_1.setBounds(59, 68, 47, 34);
        button_1.setFocusable(false);
        button_1.setContentAreaFilled(false);
        frame.getContentPane().add(button_1);

        button_2 = new JButton("3");
        button_2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (label.getText().length() == 22) {
                    Toolkit.getDefaultToolkit().beep();
                    return;
                }
                if (label.getText().equals("0") || newin) label.setText("");
                label.setText(label.getText() + "3");
                newin = false;
            }
        });
        button_2.setBounds(108, 68, 47, 34);
        button_2.setFocusable(false);
        button_2.setContentAreaFilled(false);
        frame.getContentPane().add(button_2);

        label2 = new JLabel("");
        label2.setBounds(10, 15, 260, 14);
        label2.setOpaque(true);
        label2.setBackground(Color.WHITE);
        label2.setFont(new Font("Arial", Font.BOLD, 10));
        label2.setHorizontalAlignment(SwingConstants.RIGHT);

        label = new JLabel("0");
        label.setHorizontalAlignment(SwingConstants.RIGHT);
        label.setVerticalAlignment(SwingConstants.BOTTOM);
        label.setOpaque(true);
        label.setBackground(Color.white);
        label.setBorder(border);
        label.setLayout(new BorderLayout());
        label.setFont(new Font("Arial", Font.BOLD, 20));
        label.add(label2, BorderLayout.PAGE_START);
        label.setBounds(10, 11, 265, 42);
        frame.getContentPane().add(label);
        button_3 = new JButton("4");
        button_3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                if (label.getText().length() == 22) {
                    Toolkit.getDefaultToolkit().beep();
                    return;
                }
                if (label.getText().equals("0")) label.setText("");
                label.setText(label.getText() + "4");
                newin = false;
            }
        });
        button_3.setBounds(10, 113, 47, 34);
        button_3.setFocusable(false);
        button_3.setContentAreaFilled(false);
        frame.getContentPane().add(button_3);

        button_4 = new JButton("5");
        button_4.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (label.getText().length() == 22) {
                    Toolkit.getDefaultToolkit().beep();
                    return;
                }
                if (label.getText().equals("0") || newin) label.setText("");
                label.setText(label.getText() + "5");
                newin = false;
            }
        });     
        button_4.setBounds(59, 113, 47, 34);
        button_4.setFocusable(false);
        button_4.setContentAreaFilled(false);
        frame.getContentPane().add(button_4);

        button_5 = new JButton("6");
        button_5.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (label.getText().length() == 22) {
                    Toolkit.getDefaultToolkit().beep();
                    return;
                }
                if (label.getText().equals("0") || newin) label.setText("");
                label.setText(label.getText() + "6");
                newin = false;
            }
        });
        button_5.setBounds(108, 113, 47, 34);
        button_5.setFocusable(false);
        button_5.setContentAreaFilled(false);
        frame.getContentPane().add(button_5);

        button_6 = new JButton("7");
        button_6.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (label.getText().length() == 22) {
                    Toolkit.getDefaultToolkit().beep();
                    return;
                }
                if (label.getText().equals("0") || newin) label.setText("");
                label.setText(label.getText() + "7");
                newin = false;
            }
        });
        button_6.setBounds(10, 160, 47, 34);
        button_6.setFocusable(false);
        button_6.setContentAreaFilled(false);
        frame.getContentPane().add(button_6);

        button_7 = new JButton("8");
        button_7.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (label.getText().length() == 22) {
                    Toolkit.getDefaultToolkit().beep();
                    return;
                }
                if (label.getText().equals("0") || newin) label.setText("");
                label.setText(label.getText() + "8");
                newin = false;
            }
        });
        button_7.setBounds(59, 160, 47, 34);
        button_7.setFocusable(false);
        button_7.setContentAreaFilled(false);
        frame.getContentPane().add(button_7);

        button_8 = new JButton("9");
        button_8.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (label.getText().length() == 22) {
                    Toolkit.getDefaultToolkit().beep();
                    return;
                }
                if (label.getText().equals("0") || newin) label.setText("");
                label.setText(label.getText() + "9");
                newin = false;
            }
        });
        button_8.setBounds(108, 160, 47, 34);
        button_8.setFocusable(false);
        button_8.setContentAreaFilled(false);
        frame.getContentPane().add(button_8);

        button_9 = new JButton("0");
        button_9.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (label.getText().length() == 22) {
                    Toolkit.getDefaultToolkit().beep();
                    return;
                }
                if (label.getText().equals("0") || newin) label.setText("");
                label.setText(label.getText() + "0");
                newin = false;
            }
        });
        button_9.setFocusable(false);
        button_9.setContentAreaFilled(false);
        button_9.setBounds(59, 205, 47, 34);
        frame.getContentPane().add(button_9);

        JButton button_10 = new JButton("±");
        button_10.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                if (label.getText().equals("0")) return;
                if (label.getText().substring(0, 1).equals("-")) label.setText(label.getText().substring(1));
                else label.setText("-" + label.getText());
            }
        });
        button_10.setFocusable(false);
        button_10.setContentAreaFilled(false);
        button_10.setBounds(165, 68, 49, 34);
        frame.getContentPane().add(button_10);

        button_11 = new JButton("-");
        button_11.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (newin && !label2.getText().equals("")) {
                    String in = reverseString(label2.getText());
                    in = in.substring(1);
                    in = "-" + in;
                    label2.setText("");
                    label2.setText(reverseString(in));
                    arg = "-";
                } else {
                label2.setText(label2.getText() + "  " + label.getText() + "  -");
                calculate();
                arg = "-";
                label.setText(removeZeros(result));
                newin = true;
                }
            }
        });
        button_11.setFocusable(false);
        button_11.setContentAreaFilled(false);
        button_11.setBounds(165, 113, 49, 34);
        frame.getContentPane().add(button_11);

        button_12 = new JButton("+");
        button_12.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (newin && !label2.getText().equals("")) {
                    String in = reverseString(label2.getText());
                    in = in.substring(1);
                    in = "+" + in;
                    label2.setText("");
                    label2.setText(reverseString(in));
                    arg = "+";
                } else {
                label2.setText(label2.getText() + "  " + label.getText() + "  +");
                calculate();
                arg = "+";
                label.setText(removeZeros(result));
                newin = true;
                }
            }
        });
        button_12.setFocusable(false);
        button_12.setContentAreaFilled(false);
        button_12.setBounds(165, 157, 49, 82);
        frame.getContentPane().add(button_12);

        button_13 = new JButton(".");
        button_13.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                for (int i = 0; i < label.getText().length(); i++) {
                    char a = label.getText().charAt(i);
                    if (a == '.') return;
                }
                label.setText(label.getText() + ".");
            }
        });
        button_13.setFocusable(false);
        button_13.setContentAreaFilled(false);
        button_13.setBounds(108, 205, 47, 34);
        frame.getContentPane().add(button_13);

        button_14 = new JButton("x");
        button_14.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                if (newin && !label2.getText().equals("")) {
                    String in = reverseString(label2.getText());
                    in = in.substring(1);
                    in = "x" + in;
                    label2.setText("");
                    label2.setText(reverseString(in));
                    arg = "*";
                } else {
                label2.setText(label2.getText() + "  " + label.getText() + " x");
                calculate();
                arg = "*";
                label.setText(removeZeros(result));
                newin = true;
                }
            }
        });
        button_14.setFocusable(false);
        button_14.setContentAreaFilled(false);
        button_14.setBounds(224, 160, 49, 34);
        frame.getContentPane().add(button_14);

        button_15 = new JButton("/");
        button_15.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                if (newin && !label2.getText().equals("")) {
                    String in = reverseString(label2.getText());
                    in = in.substring(1);
                    in = "/" + in;
                    label2.setText("");
                    label2.setText(reverseString(in));
                    arg = "/";
                } else {
                label2.setText(label2.getText() + "  " + 
                removeZeros(Double.valueOf(label.getText())) + "  /");
                calculate();
                arg = "/";
                label.setText(removeZeros(result));
                newin = true;
                }
            }
        });
        button_15.setFocusable(false);
        button_15.setContentAreaFilled(false);
        button_15.setBounds(224, 205, 49, 34);
        frame.getContentPane().add(button_15);

        button_16 = new JButton("C");
        button_16.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                label.setText("0");
                label2.setText("");
                result = 0.0;
                arg = "";
                newin = false;
            }
        });
        button_16.setFocusable(false);
        button_16.setToolTipText("Press \"C\" instead");
        button_16.setContentAreaFilled(false);
        button_16.setBounds(224, 113, 49, 34);
        frame.getContentPane().add(button_16);

        button_17 = new JButton("=");
        button_17.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                calculate();
                label.setText(removeZeros(result));
                label2.setText("");
                arg = "";
                newin = true;
                result = 0.0;
            }
        });
        button_17.setFocusable(false);
        button_17.setContentAreaFilled(false);
        button_17.setBounds(10, 205, 47, 34);
        frame.getContentPane().add(button_17);

    button_22 = new JButton("1/x");
        button_22.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

             label2.setText(("reciproc"+ "("+label.getText()+")"));
                inverse = 1/Double.parseDouble(label.getText());
                label.setText(removeZeros(inverse));

            }
        });
        button_22.setFocusable(false);
        button_22.setContentAreaFilled(false);
        button_22.setBounds(76, 270, 63, 60);
        frame.getContentPane().add(button_22);

    }

    private void calculate() {
        if (result == 0) {
            result = Double.valueOf(label.getText());
            return;
        }
        switch (arg) {
        case "+" : result += Double.parseDouble(label.getText()); 
        break;
        case "-" : result -= Double.parseDouble(label.getText());
        break;
        case "*" : result *= Double.valueOf(label.getText());
        break;
        case "/" : result /= Double.valueOf(label.getText());
        break;
        }   
    }

    private String removeZeros(Double n) {
        String in = String.valueOf(n);
        while (in.indexOf(".") != -1) {
            if (in.substring(in.length() - 1).equals("0") ||
                    in.substring(in.length() - 1).equals("."))
                in = in.substring(0, in.length() - 1);
            else break;
        }
        return in;
    }

    private String reverseString(String input) {
        String output = "";
        for (int i = input.length() - 1; i > -1; i--) {
            output += input.charAt(i);
        }
        return output;
    }
}

问题来源:Stack Overflow

展开
收起
montos 2020-03-24 16:44:48 640 0
1 条回答
写回答
取消 提交回答
  • 问题在于,当您输入零然后再进行任何操作然后输入操作数时,您的“结果”变量为零,因此“ calculate()”会像未输入任何内容一样对待这种情况,只需设置“结果”到新操作数,而不是执行“ 0(操作)(操作数)”。

    如果添加一个新的布尔变量“ isClear”(最初设置为true),则可以解决该问题。每次按下按钮进行某些操作时,都将此变量设置为false。在您的calculate()方法中,替换

     if (result == 0) {
          result = Double.valueOf(label.getText());
          return;
        }
    

        if (isClear) {
          result = Double.valueOf(label.getText());
          return;
        }
    

    当您按“ C”按钮清除输入时,还应将“ isClear”设置为true。

    回答来源:Stack Overflow

    2020-03-24 16:45:32
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
15分钟打造你自己的小程序 立即下载
小程序 大世界 立即下载
《15分钟打造你自己的小程序》 立即下载