private static void Demo3() {
Stack<Character> st1=new Stack();
System.out.println("请输入一行字符串:");
Scanner sc1= new Scanner(System.in);
String str = sc1.nextLine();
// System.err.println(str);
for (int i = 0; i < str.length(); i++) {
char ch=str.charAt(i);
switch(ch) {
case '{':
case '[':
case '(':
st1.push(ch);
break;
case '}':
case ']':
case ')':
if (!st1.isEmpty()) {
char chx=st1.pop();
if ((ch=='{'&&chx!='}')||(ch=='['&&chx!=']')||(ch=='('&&chx!=')')) {
System.out.println("Error" + ch+i);
}
} else {
System.out.println("Error" + ch+i);
}
break;
default: break;
}
}
if (!st1.isEmpty()) {
System.out.println("Error" +st1.pop());
}
// while (!st1.isEmpty()) {
// System.out.println(st1.pop());
// }
}
大佬NB