我在创建一个简单的计算器时遇到一些问题,但是当问一个问题(如2-1)时,系统将返回0.0。我认为我没有使用一种很好的方法来分析信息。这就是我尝试在文本框内的数字上设置双精度't'的方式。

insert = new JTextField();
insert.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        try {
            i = insert.getText();
            t = Double.parseDouble(i);
        } finally {}
    }; {}
});
insert.setBackground(new Color(135, 206, 235));
insert.setText("0");
insert.setFont(new Font("Tahoma", Font.PLAIN, 18));
insert.setColumns(10);
insert.setBounds(0, 50, 292, 28);
frame.getContentPane().add(insert);


如果错误不存在,则为完整代码:

import java.awt.EventQueue;

import javax.swing.JFrame;
import java.awt.Color;
import javax.swing.JTextField;
import java.awt.BorderLayout;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JTabbedPane;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.InputMethodListener;
import java.util.Scanner;
import java.awt.event.InputMethodEvent;

public class Window {
    double t, x, a, l, s;
    String w, i;
    Scanner dati = new Scanner(System. in );
    private JFrame frame;
    private JTextField result;
    private JTextField insert;
    private JTextField hint;
    private JTextField score;
    private final JButton help = new JButton("?");
    private final JButton calculator = new JButton("=");

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

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

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.getContentPane().setBackground(new Color(135, 206, 235));
        frame.getContentPane().setLayout(null);
        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;
            }
        });
        calculator.setBounds(292, 57, 52, 20);
        frame.getContentPane().add(calculator);
        calculator.setBackground(new Color(135, 206, 235));
        help.setBackground(new Color(135, 206, 235));
        help.setBounds(292, 0, 52, 20);
        frame.getContentPane().add(help);

        score = new JTextField();
        score.setEditable(false);
        score.setText("\u0160eit rezult\u0101ts");
        score.setColumns(10);
        score.setBounds(344, 0, 100, 20);
        frame.getContentPane().add(score);

        hint = new JTextField();
        hint.setEditable(false);
        hint.setText("\u0160eit tavi skait\u013Ci");
        hint.setBounds(344, 57, 100, 20);
        frame.getContentPane().add(hint);
        hint.setColumns(10);

        insert = new JTextField();
        insert.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    i = insert.getText();
                    t = Double.parseDouble(i);
                } finally {}
            }; {}
        });
        insert.setBackground(new Color(135, 206, 235));
        insert.setText("0");
        insert.setFont(new Font("Tahoma", Font.PLAIN, 18));
        insert.setColumns(10);
        insert.setBounds(0, 50, 292, 28);
        frame.getContentPane().add(insert);

        result = new JTextField();
        result.setEditable(false);
        result.setBackground(new Color(135, 206, 235));
        result.setBounds(0, 0, 292, 28);
        result.setText("Hei!");
        result.setFont(new Font("Tahoma", Font.PLAIN, 18));
        frame.getContentPane().add(result);
        result.setColumns(10);

        JButton add = new JButton("+");
        add.setBackground(new Color(135, 206, 250));
        add.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                if (a != 0) {
                    x = x + t;
                    t = 0;
                    l = 1;
                } else {
                    x = t;
                    t = 0;
                    a = 1;
                    l = 1;
                }
            }
        });
        add.setBounds(0, 28, 89, 23);
        frame.getContentPane().add(add);

        JButton subtract = new JButton("-");
        subtract.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (a != 0) {
                    x = x - t;
                    t = 0;
                    l = 2;
                } else {
                    x = t;
                    t = 0;
                    a = 1;
                    l = 2;
                }
            }
        });
        subtract.setBackground(new Color(135, 206, 235));
        subtract.setBounds(89, 28, 89, 23);
        frame.getContentPane().add(subtract);

        JButton multiply = new JButton("x");
        multiply.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (a != 0) {
                    x = x * t;
                    t = 0;
                    l = 3;
                } else {
                    x = t;
                    t = 0;
                    a = 1;
                    l = 3;
                }
            }
        });
        multiply.setBackground(new Color(135, 206, 235));
        multiply.setForeground(new Color(0, 0, 0));
        multiply.setBounds(178, 28, 89, 23);
        frame.getContentPane().add(multiply);

        JButton divide = new JButton("/");
        divide.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (a != 0) {
                    x = x / t;
                    t = 0;
                    l = 4;
                } else {
                    x = t;
                    t = 0;
                    a = 1;
                    l = 4;
                }
            }
        });
        divide.setBackground(new Color(135, 206, 235));
        divide.setBounds(266, 28, 89, 23);
        frame.getContentPane().add(divide);

        JButton percent = new JButton("%");
        percent.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (a != 0) {
                    x = (x / 100) * t;
                    t = 0;
                    l = 5;
                } else {
                    x = t;
                    t = 0;
                    a = 1;
                    l = 5;
                }
            }
        });
        percent.setBackground(new Color(135, 206, 235));
        percent.setBounds(355, 28, 89, 23);
        frame.getContentPane().add(percent);
        frame.setBounds(100, 100, 460, 115);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        dati.close();
    }
}


可能我正在使用错误的方式进行计算。

如果您需要一个简单的解释,我正在尝试创建一个具有五个功能的计算器-+,-,*,/,%-并且有一个文本框可插入您要计算的数字-插入,单击功能按钮,插入,单击功能按钮,完成后-=按钮。 '?'该按钮计划提供计算器的描述。顺便说一句,我正在为一个学校项目做它。

提前致谢。

附言Kļūdaprogrammā转换为程序错误。

最佳答案

在您的actionListener上,在;语句的每个{之前都有一个if。另外,您的条件很奇怪,请从中进行更改。并添加else语句或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;
    }
});


对此:

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;
    }
});


或这个(推荐):

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;
    }
});


另外建议避免使用Null Layout,而应使用Layout Manager或它们的组合。由于Swing旨在与它们一起使用,因此可以防止错误。有关此的更多信息,请阅读Why is it frowned upon to use a null layout in swing。还请检查The use of multiple JFrames, Good / Bad Practice



编辑

要从程序中获得非常相似的视图并使它正常工作,您可以尝试类似的方法,该方法不使用空布局,并且每次单击+按钮时都将打印该值。其余所有按钮尚不起作用(请参阅我的代码如何使用这些按钮,并将此逻辑应用于其余按钮):

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();
    }
}


正如您所看到的,输出非常相似:

java - 计算器对所有询问的问题均返回0.0-LMLPHP

09-10 07:19
查看更多