Java 为什么它会说堆栈是空的,即使我将某些东西推入堆栈中?

Java 为什么它会说堆栈是空的,即使我将某些东西推入堆栈中?,java,Java,出于某种原因,当我调用stack1.empty()或stack1.isEmpty()时,即使我将某些内容推入堆栈,它也会返回true?下面是我用来将某些内容推入堆栈的方法 //Case B: else if(currentChar == '('){ Character charObj = Character.valueOf('('); stack1.push(charObj); System.out.println("Case B"

出于某种原因,当我调用stack1.empty()或stack1.isEmpty()时,即使我将某些内容推入堆栈,它也会返回true?下面是我用来将某些内容推入堆栈的方法

//Case B:
     else if(currentChar == '('){
         Character charObj = Character.valueOf('(');
         stack1.push(charObj);
         System.out.println("Case B");
     }
基本上,它遍历字符串中的每个字符,并执行我编写的一种情况。在本例中,字符是一个“(”,因此我将其推入堆栈

现在,字符串中的下一个字符是字母,因此这种情况称为:

//Case A:
     if(currentChar != '+' && currentChar != '-' && currentChar != '*' && currentChar != '/' && currentChar != '(' && currentChar != ')' ){
         postfixString += currentChar;
         System.out.println("Case A");
     }
该方法工作正常。由于某种原因,下一部分出错。字符串中的下一个东西是*。因此,它应该运行我编写的特定案例,但它运行的是不同的案例…下面是它运行的案例:

//Case C:
     else if(stack1.empty() && currentChar == '+' || currentChar == '-' || currentChar == '*' || currentChar == '/'){
         stack1.push(currentChar);
         System.out.println("Case C");
     }
如您所见,运行此案例的唯一方法是如果堆栈为空,但它不是空的!我将某些内容推入堆栈中…我不明白为什么它会继续运行此案例,即使堆栈不是空的

我希望它运行此案例:

     //Case D: If character is an operator, it goes into a loop checking if topstack is higher precedence to the current character
     // If it is, the stack pops onto the end of the postfix string. If it isn't, the stack pushes the current scanned character.
     // It then breaks out of the loop
     else if(currentChar == '+' || currentChar == '-' || currentChar == '*' || currentChar == '/' && !stack1.isEmpty()){
         char topStack = stack1.peek();

        while(!stack1.isEmpty()){
            if(precedence(topStack, currentChar)){
                postfixString += stack1.pop();
            }
            else{
                stack1.push(currentChar);
                break;
            }

            System.out.println("Case D");

        }



     }
我的意思是它应该运行案例D,但它却运行案例C。它为什么这样做

编辑:

下面是全班同学:

import java.util.Stack;

public class InfixToPostfixConverter
{
//**********************************************************************
//The precedence method determines the precedence between two operators.
//If the first operator is of higher or equal precedence than the second
//operator, it returns the value true, otherwise it returns false.
//***********************************************************************
   public static boolean precedence(char topStack, char currentChar)
   {
   if(topStack == currentChar){
       return true;
   }

   // If topStack is division or multiplication, it will always have precedence no matter what
   if(topStack == '/' || topStack == '*'){
       return true;
   }
   // If topStack is addition or subtraction...
   else if(topStack == '+' || topStack == '-'){
       if(currentChar == '+' || currentChar == '-'){
           return true;
       }
       else if(currentChar == '*' || currentChar == '/'){
           return false;
       }
   }

   return false;
   }

//*************************************************************************
//The static convertToPostfix method will convert the infixString
//into the corresponding postfix string. Check the algorithm on
//assignment #11's description page. Mark each case clearly inside the code
//*************************************************************************
   public static String convertToPostfix(String infixString)
   {
  //initialize the resulting postfix string
  String postfixString = "";

  //initialize the stack
  Stack<Character> stack1 = new Stack<Character>();

 //Obtain the character at index i in the string
  for (int i=0; i < infixString.length(); i++)
  {
     char currentChar = infixString.charAt(i);

    //Case A:
     if(currentChar != '+' && currentChar != '-' && currentChar != '*' && currentChar != '/' && currentChar != '(' && currentChar != ')' ){
         postfixString += currentChar;
         System.out.println("Case A");
     }

    //Case B:
     else if(currentChar == '('){
         stack1.push(currentChar);
         System.out.println("Case B");
     }

     else if(currentChar == '+' || currentChar == '-' || currentChar == '*' || currentChar == '/'){
         //Case C
         if(stack1.isEmpty()){
             stack1.push(currentChar);
             System.out.println("Case C");
         }
         //Case D
         else{
             char topStack = stack1.peek();

             while(!stack1.isEmpty()){
                if(precedence(topStack, currentChar)){
                    postfixString += stack1.pop();
                }
                else{
                    stack1.push(currentChar);
                    break;
                }

                System.out.println("Case D");

            }
         }
     }
    //Case E:
     else if(currentChar == ')' && !stack1.isEmpty()){
         while(!stack1.isEmpty() && stack1.peek() != '('){
             postfixString += stack1.pop();
             System.out.println("Case E");
         }
         if(!stack1.isEmpty() && stack1.peek() == '('){
             stack1.pop();
         }
     }


  } //end of for loop


    //Case F:
  if(!stack1.isEmpty() && stack1.peek() == '('){
      return "No matching close parenthesis error.";
  }
  else if(!stack1.isEmpty() && stack1.peek() != '('){
      while(!stack1.isEmpty() && stack1.peek() != '('){
          postfixString += stack1.pop();
      }
  }

  System.out.println("Case F");
  return postfixString;


}//end of convertToPostfix method

}//end of the InfixToPostfixConverter class
import java.util.Stack;
公共类infixtopostfix转换器
{
//**********************************************************************
//优先级方法确定两个运算符之间的优先级。
//如果第一个运算符的优先级高于或等于第二个运算符
//运算符,则返回值true,否则返回false。
//***********************************************************************
公共静态布尔优先级(char-topStack、char-currentChar)
{
if(topStack==currentChar){
返回true;
}
//如果topStack是除法或乘法,则无论发生什么情况,它始终具有优先权
如果(上标=='/'| |上标=='*')){
返回true;
}
//如果topStack是加法或减法。。。
else如果(topStack='+'| | topStack=='-'){
如果(currentChar=='+'| | currentChar=='-'){
返回true;
}
else if(currentChar=='*'| | currentChar=='/')){
返回false;
}
}
返回false;
}
//*************************************************************************
//静态convertToPostfix方法将转换infixString
//输入相应的后缀字符串。请检查上的算法
//作业#11的描述页。在代码内清楚地标记每个案例
//*************************************************************************
公共静态字符串convertToPostfix(字符串infixString)
{
//初始化生成的后缀字符串
字符串postfix字符串=”;
//初始化堆栈
Stack stack1=新堆栈();
//获取字符串中索引i处的字符
对于(int i=0;i
运算符
&&
的优先级高于
|
,因此在此语句中:

else if(stack1.empty() && currentChar == '+' || currentChar == '-' || currentChar == '*' || currentChar == '/'){
else if (currentChar == '+' || currentChar == '-' || ...) {
    if (stack1.empty()) {
    // Case C

    } else {
    // Case D

    }
}
上述内容相当于:

else if( ( stack1.empty() && currentChar == '+' ) || currentChar == '-' || currentChar == '*' || currentChar == '/'){
举一个简单的例子:

if (a && b || c)
相当于

if ( (a && b) || c )
当有疑问时,添加paren以明确操作顺序,以便您(和其他阅读代码的程序员)清楚您的意图

解释 对于你的整个问题,这意味着你的
stack1
可能不是空的。在
中,如果我在上面引用了
stack1.empty()&¤tChar=='+'
两者都必须为true。由于情况并非如此,因此将对下一个术语进行求值,直到它到达
currentChar=='*'
,这true,因此它运行案例C

您的案例D永远不会为真,因为案例C已经检查了相同的字符。将无法访问案例D

假设案例C的意思是“stack1为空”和“currentChar是+、-、*、或/”中的一个”,那么您需要这样写:

else if (stack1.empty() && (currentChar == '+' || ...)) {
但由于您每次都检查相同的字符,我个人会使用多级
if
语句:

else if(stack1.empty() && currentChar == '+' || currentChar == '-' || currentChar == '*' || currentChar == '/'){
else if (currentChar == '+' || currentChar == '-' || ...) {
    if (stack1.empty()) {
    // Case C

    } else {
    // Case D

    }
}

&&
运算符的优先级高于
|
,因此在此语句中:

else if(stack1.empty() && currentChar == '+' || currentChar == '-' || currentChar == '*' || currentChar == '/'){
else if (currentChar == '+' || currentChar == '-' || ...) {
    if (stack1.empty()) {
    // Case C

    } else {
    // Case D

    }
}
上述情况相当于t