Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
简单Java计算器逻辑_Java - Fatal编程技术网

简单Java计算器逻辑

简单Java计算器逻辑,java,Java,我是编程新手,我正在制作一个只有1个文本字段的计算器。 我需要一个能够识别这些(+,-,*,/)中哪些字符串的方法 将结果发送到变量,当我单击=按钮时,它会显示结果。 我曾尝试编写类似于(1+2)的内容,并将其保存到变量中,然后当我尝试按=按钮设置变量时,它会显示一个特权错误 这是密码 JButton btnOne = new JButton("1"); btnOne.addActionListener(new ActionListener() { public

我是编程新手,我正在制作一个只有1个文本字段的计算器。 我需要一个能够识别这些(+,-,*,/)中哪些字符串的方法 将结果发送到变量,当我单击=按钮时,它会显示结果。 我曾尝试编写类似于(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)**

任何建议???

异常的原因是您试图将类似
“1+2”
的字符串解析为以下行中的整数:

x=Integer.parseInt(txtoptn.getText())

您应该将数字存储在其他地方,即ArrayList中

第一个问题 当您尝试只写一个“1+2”的字符串语句时,您期望会发生什么?不能简单地这样评价。在数字插入完成后(例如,当您单击+/-//*、或=)时,您必须首先对每个#按钮使用
Integer.parseInt()
。这意味着数字已完成,您将得到
Integer.parseInt(“123”)而不是Integer.parseInt(“1”)+Integer.parseInt(“2”)+Integer.parseInt(“3”)
你要做的是在解析过程中解析A+。将整数解析保持在其他位置(在第二个问题中解释)

中的数字和运算符。 第二个问题 当您说“+”时,它不会解析为任何操作。您必须将值存储到它们各自的变量中,当单击+按钮时,您将得到正确的答案。如果您希望在不进行任何外部导入的情况下执行此操作,则必须将操作存储在列表中,并在单击=时进行计算

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;

编辑:顺便说一句,尽管第二种方法快得多,但您可能应该尝试在不使用外部导入的情况下编写它,因为您是编程新手。不可避免的情况下,您将不得不考虑这个问题。

privilage error?如果您看到错误问题,请发布代码并发布错误消息。这就是code和错误好吧,您正在尝试使用
Integer.parseInt
将字符串
“1+2”
转换为整数,明显的结果是出现NumberFormatException…这不是一个“特权错误”(从未听说过),但这是一个NumberFormatException,这是一个很大的区别。您需要自己解析字符串,提取数字和运算符——这不是一件非常简单的事情,或者使用Rhino之类的工具来为您完成这项工作。使用脚本引擎解析字符串表达式是一个非常有趣的解决方案。这太好了!@trooper谢谢:)我最近碰巧用过。