判断括号匹配:
package suanfa;
import java.util.Scanner;
public class checkparahencepair {
public boolean chechmatcher(char a,char b){
if(a=='('&&b==')')
return true;
if(a=='['&&b==']')
return true;
if(a=='{'&&b=='}')
return true;
return false;
}
public boolean whethermatcher(String s){
char[] store=new char[s.length()];
int top=-1;
char[]temp=s.toCharArray();
for(char a:temp){
if(a=='('||a=='{'||a=='['){
store[++top]=a;
}
try {
if (a == ')' || a == '}' || a == ']') {
if (chechmatcher(store[top], a)) {
top--;
} else {
return false;
}
}
} catch (ArrayIndexOutOfBoundsException e) {
// TODO Auto-generated catch block
return false;
}
}
if(top==-1){
return true;
}
return false;
}
public static void main(String[] args) {
checkparahencepair cc=new checkparahencepair();
Scanner scan=new Scanner(System.in);
while(true){
String s=scan.next();
System.out.println("the text you put in is :"+s+" answer is "
+cc.whethermatcher(s));
}
}
}