繁体   English   中英

简单的Java计算器逻辑

[英]Simple Java Calculator Logic

我是编程新手,我当时只用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”的字符串语句时,您期望发生什么? 不能仅仅这样评估它。 完成数字插入后,您必须首先对每个#按钮使用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+2"类的字符串解析为该行中的Integer:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM