计算器对所有询问的问题均返回0.0 [英] Calculator returns 0.0 to all questions asked

查看:38
本文介绍了计算器对所有询问的问题均返回0.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  insert = new JTextField();insert.addActionListener(new ActionListener(){公共无效actionPerformed(ActionEvent e){尝试 {我= insert.getText();t = Double.parseDouble(i);} 最后 {}};{}});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;导入javax.swing.JFrame;导入java.awt.Color;导入javax.swing.JTextField;导入java.awt.BorderLayout;导入java.awt.Font;导入javax.swing.JButton;导入javax.swing.JTabbedPane;导入java.awt.event.ActionListener;导入java.awt.event.ActionEvent;导入java.awt.event.InputMethodListener;导入java.util.Scanner;导入java.awt.event.InputMethodEvent;公共类Window {双倍t,x,a,l,s;字符串w,i;Scanner dati =新的Scanner(System.in);私有JFrame框架;私有JTextField结果;私有JTextField插入;私有JTextField提示;私人JTextField得分;私有的最终JButton帮助=新的JButton(?");私有的最终JButton计算器= new JButton("=");/***启动应用程序.*/公共静态void main(String [] args){EventQueue.invokeLater(new Runnable(){公共无效run(){尝试 {Window window = new Window();window.frame.setVisible(true);} catch(Exception e){e.printStackTrace();}}});}/***创建应用程序.*/公共窗口(){初始化();}/***初始化框架的内容.*/私人void initialize(){frame = new JFrame();frame.getContentPane().setBackground(new Color(135,206,235));frame.getContentPane().setLayout(null);Calculator.addActionListener(new ActionListener(){公共无效actionPerformed(ActionEvent e){如果(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 l == 4& l l == 3);{s =(x/100)* t;}result.setText(Double.toString(s));} 别的 {result.setText(Kļūdaprogrammā:(");}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);分数=新的JTextField();score.setEditable(false);score.setText("\ u016​​0eit rezult \ u0101ts");score.setColumns(10);score.setBounds(344,0,100,20);frame.getContentPane().add(score);提示=新的JTextField();hint.setEditable(false);hint.setText("\ u016​​0eit tavi skait \ u013Ci");hint.setBounds(344,57,100,20);frame.getContentPane().add(hint);hint.setColumns(10);插入=新的JTextField();insert.addActionListener(new ActionListener(){公共无效actionPerformed(ActionEvent e){尝试 {我= insert.getText();t = Double.parseDouble(i);} 最后 {}};{}});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);结果=新的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 =新的JButton("+");add.setBackground(new Color(135,206,250));add.addActionListener(new ActionListener(){公共无效actionPerformed(ActionEvent arg0){如果(a!= 0){x = x + t;t = 0;l = 1;} 别的 {x = t;t = 0;a = 1;l = 1;}}});add.setBounds(0,28,89,23);frame.getContentPane().add(add);JButton减去= new JButton(-");减去.addActionListener(new ActionListener(){公共无效actionPerformed(ActionEvent e){如果(a!= 0){x = x-t;t = 0;l = 2;} 别的 {x = t;t = 0;a = 1;l = 2;}}});减去.setBackground(新颜色(135,206,235));减去.setBounds(89,28,89,23);frame.getContentPane().add(subtract);JButton乘法=新的JButton("x");multiple.addActionListener(new ActionListener(){公共无效actionPerformed(ActionEvent e){如果(a!= 0){x = x * t;t = 0;l = 3;} 别的 {x = t;t = 0;a = 1;l = 3;}}});multiple.setBackground(new Color(135,206,235));multiple.setForeground(new Color(0,0,0));multiple.setBounds(178,28,89,23);frame.getContentPane().add(multiply);JButton除法=新的JButton("/");split.addActionListener(new ActionListener(){公共无效actionPerformed(ActionEvent e){如果(a!= 0){x = x/t;t = 0;l = 4;} 别的 {x = t;t = 0;a = 1;l = 4;}}});split.setBackground(new Color(135,206,235));Divor.setBounds(266,28,89,23);frame.getContentPane().add(divide);JButton百分比=新的JButton(%");percent.addActionListener(new ActionListener(){公共无效actionPerformed(ActionEvent e){如果(a!= 0){x =(x/100)* t;t = 0;l = 5;} 别的 {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();}} 

可能我使用的是错误的计算方式.

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

谢谢.

P.S.Kļūdaprogrammā转换为程序错误.

解决方案

在您的 actionListener 上,您在每个 {上都有一个; if 语句.另外,您的条件很奇怪,请从中进行更改.并添加 else 语句或 switch

  calculator.addActionListener(new ActionListener(){公共无效actionPerformed(ActionEvent e){如果(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 l == 4& l l == 3);{s =(x/100)* t;}result.setText(Double.toString(s));} 别的 {result.setText(Kļūdaprogrammā:(");;}t = 0;x = 0;l = 0;a = 0;}}); 

对此:

  calculator.addActionListener(new ActionListener(){公共无效actionPerformed(ActionEvent e){如果(l< 6& l> 0){如果(l == 1){s = x + t;}否则,如果(l == 2){s = x-t;}否则,如果(l == 3){s = x * t;}否则,如果(l == 4){s = x/t;}否则,如果(l == 5){s =(x/100)* t;}result.setText(Double.toString(s));} 别的 {result.setText(Kļūdaprogrammā:(");;}t = 0;x = 0;l = 0;a = 0;}}); 

或者这一个(推荐):

  calculator.addActionListener(new ActionListener(){公共无效actionPerformed(ActionEvent e){开关(l){情况1:s = x + t;result.setText(Double.toString(s));休息;情况2:s = x-t;result.setText(Double.toString(s));休息;情况3:s = x * t;result.setText(Double.toString(s));休息;情况4:s = x/t;result.setText(Double.toString(s));休息;情况5:s =(x/100)* t;result.setText(Double.toString(s));休息;默认:result.setText(Kļūdaprogrammā:(");;休息;}t = 0;x = 0;l = 0;a = 0;}}); 

作为一个建议,避免使用Null Layout,而应使用

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 a good way to analise information. This is how I try to set the double 't' to the number inside the textbox.

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

If the mistake is not there, this is the full code:

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

Possibly I am using a bad way to calculate.

If you want a simple explanation, I am trying to create a calculator with five functions - +, -, *, /, % - and has one text box to insert the numbers you want to calculate - insert, click function button, insert, click function button, when done - the = button. '?' button is planned to give a description of the calculator. I am making it for a school project, by the way.

Thanks in advance.

P.S. Kļūda programmā translates to fault in program.

解决方案

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆