如何更正错误 [英] How do I correct the error

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

问题描述

/**
   This Program demonstrates how to calculate the expression in infix form.
   Mansi Desai
   Date: 5/6/2018
*/

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import javax.swing.*;
import java.util.Stack;

// class PreFix

public class PreFix extends JFrame implements ActionListener

{

     // Create objects

     JTextField calcIn = new JTextField(20);

     JTextField calcOut = new JTextField(20);

     // main method

     public static void main(String[] args)

     {

          PreFix frame = new PreFix();

          // Call method frontEnd()

          frame.frontEnd();

     }

     void frontEnd()

     {

         Container c = getContentPane();

         c.setLayout(new FlowLayout());
         
          // Create a label to enter formula

         c.add(new JLabel("Enter formula:"));

         c.add(calcIn);

         // Create a button for calculate

         // expression

         JButton b1 = new JButton("Calculate");

         // when button is pressed,

         // call the action listener in this class

         b1.setActionCommand("calculate");

         b1.addActionListener(this);

         c.add(b1);

         
         // Create a label to

         // display the result

         c.add(new JLabel("Result:"));

         c.add(calcOut);

         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

         pack();

         setVisible(true);
     }

     public void actionPerformed(ActionEvent e)

     {

        try

        {

              // Create an object for Stack class

              Stack operations = new Stack();

              String[] ops = calcIn.getText().split("\\s+");

              for (String op : ops)

              {

                   // Call method add()

                   operations.add(op);

              } 

              // note there need to be at least three

              // items on the Stack e.g. + 1 2

             

              // Check operations size less than three

              if (operations.size() < 3)

              {

                   JOptionPane.showMessageDialog(rootPane, "Not "+ "enough parameters!");

                   return;

              } 

              // Assign zero to result

              Integer result = 0;

              // do the first op

              Integer p = Integer.parseInt((String)operations.pop());

              Integer q = Integer.parseInt((String)operations.pop());

              String newOp = (String) operations.pop();

              if ("+".equals(newOp))

              {

                   result = p + q;

              } // end of if

              else

              {

                   result = p * q;

              } 

              while (operations.size() > 1)

              {

                   q = Integer.parseInt((String)operations.pop());

                   newOp = (String) operations.pop();

                   if ("+".equals(newOp))

                   {
                        // add q to result

                        result = result + q;
                   } 

                   else

                   {

                        // multiply result with q

                        result = result * q;

                   } // end of else

              } // end of while

              calcOut.setText(result.toString());

          }

          // exception caught

          catch (Exception ioe)

          {

              JOptionPane.showMessageDialog(rootPane, ioe);

          } // end of catch statement

     } // end of method

} // end of class





我尝试过:



此程序适用于小数字像+2 51但显示java.lang.NumberFormatException:对于输入字符串:+。它不适用于表达式(* + 22 3 + 5 20)



请解决此问题。



What I have tried:

This program works for small numbers like +2 51 but shows java.lang.NumberFormatException: For input string: " + ". It doesn't work for expression like (* + 22 3 + 5 20)

Please solve this.

推荐答案

在计算完成后,需要将结果变量推回到堆栈中。它将是下一个操作中的第一个值。
You need to push the result variable back onto the stack after it has been calculated. It will be the first value in the next operation.


这篇关于如何更正错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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