gpt4 book ai didi

java - Balanced Parens 在 Java 中没有返回平衡

转载 作者:行者123 更新时间:2023-11-29 10:15:17 25 4
gpt4 key购买 nike

我正在测试算术公式 (Ex. ((5-6)/(3+2)*34) ) 是否有平衡括号,但我检查左右括号的循环返回的结果不相等。我正在通过在出现提示时在控制台中输入“()”来测试它。

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个右括号和0个左括号。

有什么想法吗?我写的时候听起来/看起来不错。

更新:这是我的代码更新为使用 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.

最佳答案

您不想使用 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 -变体,正如 Chris 所问,您将在哪里使用 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 -loop 也是错误的。您正在使用: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++)
...

关于java - Balanced Parens 在 Java 中没有返回平衡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19517318/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com