黑马程序员技术交流社区

标题: 判断一个字符串是否是对称字符串 [打印本页]

作者: 一顿一只牛    时间: 2014-8-6 18:09
标题: 判断一个字符串是否是对称字符串
本帖最后由 一顿一只牛 于 2014-8-7 10:43 编辑

public class Text4 {
        public static void main(String[] args){
                String str = "abda";
                com(str);
        }
        
        @SuppressWarnings("unused")
        public static void com(String str){
                int count = (str.length()-1)/2;   //判断次数为x<=count
                boolean result = true;
               
                for(int x=0;x<=count;x++){
                        
                        if(str.charAt(x)!=str.charAt(str.length()-1-x)){
                                 result = false;
                                 //break;                                
                        }
                        break;
                        }
                if(result){
                        System.out.println(str+" 是对称字符串!");
                }
                else
                        System.out.println(str+" 不是对称字符串!");
                        
        }
}
  为什么输入字符串abda  得到的输出是对称的呀。
求大神解答。
作者: fantacyleo    时间: 2014-8-6 18:18
你原先那句break是正确的,为啥注释掉了,然后在一个错误的地方又写了一个break?
作者: fxwb2005    时间: 2014-8-6 18:18
本帖最后由 fxwb2005 于 2014-8-6 18:26 编辑

循环了一次就break了……




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2