我是 Java 新手,并试图完成一个程序,该程序将读取用户的语句并扫描以查看 LEFT 括号的数量是否与 RIGHT 匹配。启动程序的人创建了一个堆栈,但从未使用过它,所以我不理会它,因为我不太擅长堆栈。但是,我能够创建一个循环来遍历字符串中的每个字符以找到括号,比较它们,然后打印出它们是否是偶数。但是,我在遍历字符串以查找所有括号的 while 循环时遇到了麻烦。由于某种原因它不起作用,我不明白为什么。任何关于如何进行这项工作的解释将不胜感激。
import java.util.*
public class ParenMatch
{
public static void main (String[] args)
{
Stack s = new Stack();
String line; // the string of characters to be checked
Scanner scan = new Scanner(System.in);
System.out.println ("\nParenthesis Matching");
System.out.print ("Enter a parenthesized expression: ");
line = scan.nextLine();
char parenline[] = new char[line.length()];
int x;
while(x < parenline.length) {
parenline[x] = line.charAt(x);
x++;
}
int l,r,i,morel,morer = 0;
while (i > parenline.length) {
if (parenline[i] == "(" )
l++;
if (line.charAt(i) == ")")
r++;
i++;
}
if (l > r) {
morel = l-r;
System.out.println("There are " +morel+ " more left parentheses than right");
}
if (r > l) {
morer = r-l;
System.out.println("There are " +morer+ " more right parentheses then left");
}
if (r == l) {
System.out.println("The amount of left and right parentheses are even.");
}
}
}