Java 计算器运算符错误 [英] Java Calculator Operator Errors

查看:29
本文介绍了Java 计算器运算符错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 Java swing 库构建了一个计算器.除了 actionEvent 循环中的乘法和除法运算符之外,其他一切都有效.所有其他 Operator 完全正常工作.

I build a calculator with Java swing library. Every thing else works except multiplication and division operators in the actionEvent loop. All other Operators work completely.

这是发生的错误:我在这部分代码中尝试了 try 语句

This is were the error occurs: I have tried a try statement on this part of the code

计算器:

计算器乘法错误:

  1. 首先输入数字

  1. First you enter the number

然后按操作符,它应该清除文本字段 - 在这一步发生错误

Then you press operator which is suppose to clear textfield - error occurs at this step

然后输入第二个数字

然后按 = 按钮输出答案

Then press = button to output answer

错误图片:

if(e.equals("*"))
{
        fnum = txt.getText();
        logic.setTotal(fnum);
        op = "*";
        txt.setText(""); // error occurs here, textfield isn't cleared
        JOptionPane.showMessageDialog(null, fnum); //messagebox to see if fnum contains the string from the textfield
}
if(e.equals("/"))
{
        fnum = txt.getText();
        op = "/";
        txt.setText("");
}

ActionEvent 循环/函数:

ActionEvent Loop/function:

public void actionPerformed(ActionEvent ea)
{
    else if(op.equals("*"))
    {
        logic.setTotal(fnum);
        logic.multiplication(snum);
        total1 = logic.total;
    }
    else if(op.equals("/"))
    {
        logic.setTotal(fnum);
        logic.divide(snum);
        total1 = logic.total;
    }
    txt.setText(""+total1);
}

逻辑是内部类

内部类:

public class Inner extends Calculators{
    public double total;
    public Inner()
    {
        total = 0;
    }
    public void setTotal(String n)
    {
        total = convertToNumber(n); 
    }
    public void divide(String n)
    {
        total /= convertToNumber(n);
    }
    public void multiplication(String n)
    {
        total *=convertToNumber(n);
    }
}

如果您感到困惑,请索取更多代码,因为我无法包含所有代码.

If you are confused please ask for more code because I can't include all of the code.

代码,如果你想自己尝试一下

推荐答案

您首先像这样创建按钮:

You are at first creating your buttons like this:

    ...
    JButton plus = new JButton("+");
    JButton multiplication = new JButton("*");
    JButton divide = new JButton("/");
    JButton minus = new JButton("-");
    ...

然后添加 this 作为动作监听器.但是缺少一些行:

and then adding this as action listener. But some lines are missing:

    ...
    plus.addActionListener(this);
    // missing: multiplication.addActionListener(this);
    // missing: divide.addActionListener(this);
    minus.addActionListener(this);
    ...

我是如何发现错误的:

  1. 下载代码、编译等
  2. 运行代码,尝试加法、乘法等(检查应用程序的行为).这是一种黑盒测试.
  3. 通过分析代码寻找加法和乘法之间的区别.这与白盒测试有关.
  4. 我看到,应该调用 JOptionPane.showMessageDialog(null, fnum); - 但尚未调用.所以我设置了一个断点(在eclipse中)用于调试.
  5. 当我意识到 actionPerformed 方法没有被调用时,我搜索了正在注册 ActionListeners 的代码行.
  1. Downloaded the code, compiled, etc.
  2. Ran the code, tried addition, multiplication, etc. (checking the behaviour of the application). This is kind of black box testing.
  3. Looked for differences between addition and multiplication by analyzing the code. This is related to white box testing.
  4. I saw, that JOptionPane.showMessageDialog(null, fnum); should be called - but has not been called. So I sat a break point (in eclipse) for debugging.
  5. When I realized, that the actionPerformed method has not been called at I, I searched for the lines of code, that are registering the ActionListeners.

除此之外:我强烈建议您重构您的代码.您可以从重新思考代码结构中受益.您将获得更好的可读性,代码将更容易维护,新功能可以更快地实现.

Besides that: I would strongly recommend to refactor your code. You could benefit from rethinking the structure of your code. You will get better readability, the code will be easier to maintain and new features can be implemented faster.

我建议:

  • 降低字段的可见性.将您的字段设为 private,以便您可以轻松找到对它们的所有引用.
  • 避免重复(称为不要重复自己技术).例如:不是为每个按钮调用 addActionListener 而是创建一个按钮集合(即 ArrayList 并使用 for 循环来调用 addActionListener 为每一个.
  • 还可以通过定义更多但更短的方法
  • 来避免重复的代码片段
  • 考虑删除您的类 Calculators 并将该代码直接放入 Inner 的方法中.
  • Inner 找到一个更有意义的名称.也许 IntermediateResult 或类似的.
  • 为每个按钮创建一个单独的 ActionListener 实例.这会消耗一点性能(人类不会注意到),但会避免长if-chains
  • 将您的代码发布到代码审查(在 StackExchange 网络中)以获得更多帮助和新想法
  • Reduce the visibility of your fields. Make your fields private, so that you can easily find all references to them.
  • Avoid repetitions (called Don't repead yourself technique). For example: Instead of calling addActionListener for each button make a collection of Buttons (i.e. ArrayList<JButton> and use a for loop to call addActionListener for each one of them.
  • Also avoid duplicated code fragments, by defining more, but shorter methods
  • Consider deleting your class Calculators and put that code directly into the methods of Inner.
  • Find a more meaningful name for Inner. Maybe IntermediateResult or similar.
  • Create a separate ActionListener instance for each button. This will cost a little bit of performance (not noticable by humans), but will avoid long if-chains
  • Post your code on Code Review (in the StackExchange network) for getting even more help and new ideas

这篇关于Java 计算器运算符错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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