平衡的Parens在java中没有平衡返回

时间:2013-10-22 12:02:19

标签: java stack

我正在测试一个算术公式(例如((5-6)/(3 + 2)* 34))是否具有平衡括号,但我检查右和左parens的循环不是等于返回。我正在测试它,只需在提示时输入“()”进入控制台。

for (int i = 0; i < formula.size(); i++)
  {
    char c = formula.pop();

    if (c == ')') {
        right++;
        break;
    } else if (c == '(') {
        left++;
        break;
    } else {
        break;
    }

  }// End for loop

  //System.out.println("There are " + left + " left parens, and " + right + " right parens.");

  if (right == left)
     System.out.println("The parentheses are balanced.");
  else
     System.out.println("The parentheses are NOT balanced.");

我的左右变量初始化为0,我得到的输出是有1个右paren和0个左parens。

有什么想法?当我写这篇文章时,它听起来很正确。

更新:这是我的代码更新为使用switch case而不是if else。仍然提供相同的输出..

for (int i = 0; i < formula.size(); i++)
  {
    char c = formula.pop();
     switch(c)
     {
        case ')':
           right++;
           break;   //Which breaks the switch, not the for
        case '(':
           left++;
           break;   //We don't need to do anything if it's neither.

     }// End switch      
  }// End for loop

更新#2:以下是我最近的主要变化:

public static void main(String[ ] args) {

  //variables
  String formulaString;
  Stack<Character> formula = new Stack<Character>();
  int right = 0;
  int left = 0;

  Scanner in = new Scanner(System.in);

  System.out.println("Welcome, enter a mathmatical formula and I will "
                    + "determine if the parentheses are balanced.\n");

  formulaString = in.next();

  for (int j = 0; j < formulaString.length(); j++) {

     formula.push(formulaString.charAt(j));

  }// End for loop

  System.out.println("Preview of the formula just entered: ");
  System.out.println(formula.display());

  System.out.println("The size of the stack is: " + formula.size());
  System.out.println("/******************************************");

  for (int i = 0; i <= formula.size(); i++)
  {
    char c = formula.pop();
    System.out.println(c);
    switch(c)
    {
       case ')':
         right++;
         break;   //Which breaks the switch, not the for
      case '(':
         left++;
         break;   //We don't need to do anything if it's neither.

     }// End switch      
  }// End for loop

  System.out.println("There are " + left + " left parens, and " + right + " right parens.");

  if (right == left)
     System.out.println("The parentheses are balanced.");
  else
     System.out.println("The parentheses are NOT balanced.");

}// End main.

我现在正在测试的输入是(())。 我的输出我得到了它:

Preview of the formula just entered: 
[(, (, ), )]
The size of the stack is: 4
/******************************************
)
)
(
There are 1 left parens, and 2 right parens.
The parentheses are NOT balanced.

2 个答案:

答案 0 :(得分:8)

您不想使用break,您想使用continue,但在这种情况下根本不需要它。将你的循环改为:

for (int i = 0; i < formula.size(); i++)
{
  char c = formula.pop();

  if (c == ')')
  {
    right++;
    continue; //You don't need to add this since nothing is being done after this point
  }
  else if (c == '(')
  {
    left++;
    continue; //You don't need to add this since nothing is being done after this point
  }
  //We don't need to do anything if it's neither
}

break将退出for循环,而不是继续下一个项目。因此,您只找到1个括号。

更新:正如克里斯所问,switch - 变体,您将使用break的位置,如下所示:

for (int i = 0; i < formula.size(); i++)
{
  char c = formula.pop();
  switch(c)
  {
    case '(':
      left++;
      break; //Which breaks the switch, not the for
    case ')':
      right++;
      break;
    //We don't need to do anything if it's neither. 
  }
}

更新2 : 我现在看到你的for - 循环也是错误的。您正在使用:for (int i = 0; i < formula.size(); i++)。由于您使用pop,因此每次执行循环时formula.size()都会减少,而i会增加while。因此你的循环过早结束。有两种方法可以解决这个问题。您可以像这样使用while (formula.size() > 0) ... - 循环:

for

或者您可以将int formulasize = formula.size(); for (int i = 0; i < formulasize; i++) ... - 循环更改为:

{{1}}

答案 1 :(得分:0)

您已经放置导致循环不完整的中断

if (c == ')') {
  right++;              
} else if (c == '(') {
  left++;               
}