java - PostFixCalculator while/try 错误

标签 java

import java.util.Scanner;

public class PostFixCalculator
{
      public static void main(String [] args)
      {
            Scanner kbd = new Scanner(System.in);
            int result;
            String expression;

            System.out.println("Student name, CS-304, Fall 2014, Asst 2c.");

            System.out.println("To quit this program, just hit 'return'.\n");

            System.out.print("Enter a postfix expression: ");
            expression = kbd.nextLine();

         while (!expression.equals(""))
         {
               try
               {
               }
            catch(RuntimeException e)
            {
            }

            System.out.print("\nEnter a postfix expression: ");
            expression = kbd.nextLine();

         } // end while (!expression.equals(""))

         System.out.println("\nBye!");

      } // end public static void main(String [] args)


   public static int postFixEvaluate(String input)
   {
      Scanner tokenizer; 
      int result, operand1, operand2, value;
      String operator;
      LinkedStack s = new LinkedStack();

      tokenizer = new Scanner(input);

      while (tokenizer.hasNext())
      {
         if (tokenizer.hasNextInt())
         {
            value = tokenizer.nextInt();
            s.push(value);
         }

         else // we have an operator
         {
            operator = tokenizer.next();

            if (s.isEmpty())
                throw new RuntimeException ("Not Enough Operands");
                operand2 = s.pop();

            if (s.isEmpty())
                throw new RuntimeException ("Not Enough Operands");
                operand1 = s.pop();

            if (operator.equals("+"))
               result = operand1 + operand2;
            else if (operator.equals("-"))
               result = operand1 - operand2;
            else if (operator.equals("*"))
               result = operand1 * operand2;
            else if (operator.equals("/"))
               result = operand1 / operand2;
            else
                throw new RuntimeException ("Not Enough Operands");

            s.push(result);
         } // end else // we have an operator

      } // end while (tokenizer.hasNext())

      if (s.isEmpty())
          throw new RuntimeException ("Not Enough Operands");

         result = s.pop();

      if (!s.isEmpty())
          throw new RuntimeException ("Not Enough Operands");

      return result;

   } // end public static int postFixEvaluate(String input)

} // end public class PostFixCalculator

我有这段代码,一个 PostFixCalculator,但无论我暂时输入什么并尝试,我总是收到错误。该程序按发布的方式编译和运行,但运行不正确。我撞墙了..

最佳答案

您的 while 循环不正确。尝试重新访问它并进行更正

关于java - PostFixCalculator while/try 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25877480/

相关文章:

java - 我想从以下 HashMap 中获取常见时间集的工作日

java - Firefox ESR - 在两个不同选项卡中加载同一个小程序两次时,仅打开一个虚拟机

java - 如何通过 java 代码将 java 映射转换为不可变的 Scala 映射?

java - 如何在Java中组合两个网格位置?

java - 如何按正确顺序将 LinkedHashMap 值转换为 ArrayList/Array?

java - 如何找到数组的所有连续子数组组合并打印它

java - Zebra 打印机塞尔维亚拉丁字符

java - jsp中使用session的登录页面

java - 认证和授权 jax-rs 休息服务

Java字符串分割方法