Calculator returns 0.0 to all questions asked

前端 未结 1 1801
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-22 10:40

I have some problems with the creation of a simple calculator, but when asking a question (like 2-1) the system returns 0.0 as the result. I am thinking that I am not using

相关标签:
1条回答
  • 2020-12-22 11:28

    On your actionListener you have a ; before each { on the if statements. Also, your conditions are strange, change it from this. And add else statements or a switch

    calculator.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (l < 6 && l > 0) {
                if (l != 2 && l != 3 && l != 4 && l != 5); {
                    s = x + t;
                }
                if (l != 1 && l != 3 && l != 4 && l != 5); {
                    s = x - t;
                }
                if (l != 1 && l != 2 && l != 4 && l != 5); {
                    s = x * t;
                }
                if (l != 1 && l != 2 && l != 3 && l != 5); {
                    s = x / t;
                }
                if (l != 1 && l != 2 && l != 4 && l != 3); {
                    s = (x / 100) * t;
                }
                result.setText(Double.toString(s));
            } else {
                result.setText("Kļūda programmā :(");
            }
            t = 0;
            x = 0;
            l = 0;
            a = 0;
        }
    });
    

    To this:

    calculator.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (l < 6 && l > 0) {
                if (l == 1) {
                    s = x + t;
                } else if (l == 2) {
                    s = x - t;
                } else if (l == 3) {
                    s = x * t;
                } else if (l == 4) {
                    s = x / t;
                } else if (l == 5) {
                    s = (x / 100) * t;
                }
                result.setText(Double.toString(s));
            } else {
                result.setText("Kļūda programmā :(");
            }
            t = 0;
            x = 0;
            l = 0;
            a = 0;
        }
    });
    

    Or this one (Recommended):

    calculator.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            switch (l) {
                case 1:
                    s = x + t;
                    result.setText(Double.toString(s));
                    break;
                case 2:
                    s = x - t;
                    result.setText(Double.toString(s));
                    break;
                case 3:
                    s = x * t;
                    result.setText(Double.toString(s));
                    break;
                case 4:
                    s = x / t;
                    result.setText(Double.toString(s));
                    break;
                case 5:
                    s = (x / 100) * t;
                    result.setText(Double.toString(s));
                    break;
                default:
                    result.setText("Kļūda programmā :(");
                    break;
            }
            t = 0;
            x = 0;
            l = 0;
            a = 0;
        }
    });
    

    And as an aside recommendation avoid using Null Layout, instead use a Layout Manager or combinations of them. This will prevent bugs since Swing was designed to work with them. For more information about this read Why is it frowned upon to use a null layout in swing. Also please check The use of multiple JFrames, Good / Bad Practice


    Edit

    To get a very similar view from your program and get it to work, you could try something like this, which doesn't use a null layout and prints the value each time you clic the + button. All the rest of buttons don't work yet (See how my code works with these buttons and take this logic to the rest of them):

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.*;
    public class LayoutExample {
        double number = 0;
        JFrame frame;
        JLabel label;
        JTextField field;
        JButton add, substract, mult, divide, total, help, perc;
        JPanel topPane, buttonPane, fieldPane;
        LayoutExample() {
            frame = new JFrame("Calculator");
            label = new JLabel("Result: ");
            field = new JTextField("0");
            add = new JButton("+");
            substract = new JButton("-");
            mult = new JButton("*");
            divide = new JButton("/");
            total = new JButton("=");
            perc = new JButton("%");
            help = new JButton("?");
            topPane = new JPanel();
            buttonPane = new JPanel();
            fieldPane = new JPanel();
            
            field.setColumns(10);
            
            topPane.setLayout(new FlowLayout());
            buttonPane.setLayout(new FlowLayout());
            fieldPane.setLayout(new FlowLayout());
            
            topPane.add(label);
            topPane.add(help);
            
            add.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    try {
                        number += Integer.parseInt(field.getText());
                    } catch (NumberFormatException nfe) {
                        nfe.printStackTrace();
                    }
                }
            });
            
            total.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    label.setText("Result: " + number);
                }
            });
            
            buttonPane.add(add);
            buttonPane.add(substract);
            buttonPane.add(mult);
            buttonPane.add(divide);
            buttonPane.add(perc);
            
            fieldPane.add(field);
            fieldPane.add(total);
            
            frame.add(topPane, BorderLayout.PAGE_START);
            frame.add(buttonPane, BorderLayout.CENTER);
            frame.add(fieldPane, BorderLayout.PAGE_END);
            
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
            frame.setVisible(true);
        }
        public static void main(String args[]) {
            new LayoutExample();
        }
    }
    

    The output is very similar to the one you did as you can see:

    0 讨论(0)
提交回复
热议问题