简单的Java计算器逻辑

时间:2015-09-07 20:18:12

标签: java

我是编程的新手,我只用一个文本字段制作一个计算器。 我需要一种方法,可以识别这些(+, - ,*,/)中的字符串字符 将结果发送到变量,当我单击 = 按钮时,它会显示结果。 我试着写一些类似(1 + 2)的东西并将其保存到变量然后当我尝试按 = 按钮来设置文本变量时它显示了一个特权错误

这是代码

    JButton btnOne = new JButton("1");
    btnOne.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            sum+=1;
            txtoprtn.setText(txtoprtn.getText()+"1");

    JButton btnTwo = new JButton("2");
    btnTwo.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            sum+=2;
            txtoprtn.setText(txtoprtn.getText()+"2");
        }
    });

    JButton btnAdd = new JButton("+");
    btnAdd.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            txtoprtn.setText(txtoprtn.getText()+"+");

        }
    });

    JButton btnEqual = new JButton("=");
    btnEqual.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            x = Integer.parseInt(txtoprtn.getText());
            txtoprtn.setText(Integer.toString(x));
            }

        }
    );

这是错误

    **Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "1+2"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at com.jadv.day01.tasks.AdvCalc$11.actionPerformed(AdvCalc.java:140)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)**

任何建议????

2 个答案:

答案 0 :(得分:4)

第一个问题

当你尝试只写一个“1 + 2”的字符串语句时,你会发生什么?它不能简单地像那样评估。完成号码插入后,您必须首先对每个#按钮使用Integer.parseInt()(例如,当您单击+ / - /// *或=时。这意味着号码已完成且您'我会说 Integer.parseInt("123") instead of Integer.parseInt("1")+Integer.parseInt("2")+Integer.parseInt("3") 你要做的是在解析的中间解析一个+。保持整数解析数字和运算符存储在其他地方(在第二个问题中解释)

第二个问题

当你说“+”时,它不会解析为任何操作。您必须将值存储到各自的变量中,单击+按钮时,您将得到正确的答案。如果您想在没有任何外部导入的情况下执行此操作,则必须将操作存储在列表中并评估何时单击=。

btnAdd.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            operations.add("+");//operations is a list
        }
});

使用导入一次解决两个问题

如果您想使用包评估,请使用:

ScriptEngine evaluationMachine = new ScriptEngineManager().getEngineByName("JavaScript");
engine.eval(foo); //evaluates something like "2+1" into 3.

使用以下导入:

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

编辑:顺便说一下,虽然第二种方式要快得多,但你应该尝试在没有外部导入的情况下编写它,因为你不熟悉编程。在不可避免的情况下,你将不得不考虑这个问题。

答案 1 :(得分:0)

异常的原因是你试图将"1+2"之类的字符串解析为行中的整数:

x = Integer.parseInt(txtoprtn.getText());

您应该将数字存储在其他位置,即在ArrayList中。

相关问题