判断输入的字符是不是按照{}{}{}这种格式进行输出

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("不匹配");
  }
 }
}

posted on 2013-10-15 09:46  知识海洋  阅读(169)  评论(0)    收藏  举报