判断输入的字符是不是按照{}{}{}这种格式进行输出
public class Duizhan {
public static void main(String[] args) throws Exception {
String s="{}{{}}{}{}";
try{
Stack stc=new Stack();
for(int i=0;i<s.length();i++){
char c=s.charAt(i++);
if(c=='{') stc.push(new Character('}'));
char c2=((Character) stc.pop()).charValue();
char c1=s.charAt(i);
if(c1!=c2) throw new Exception("不匹配");
}
System.out.println("匹配");
}catch(Exception e){
System.out.println("不匹配");
}
}
}
浙公网安备 33010602011771号