黑马程序员技术交流社区

标题: 判断是不是回文 哪里有问题 [打印本页]

作者: 在学野马    时间: 2014-10-7 09:13
标题: 判断是不是回文 哪里有问题
本帖最后由 在学野马 于 2014-10-7 19:46 编辑

public class hui {

        /**
         * @param args
         */
        public static void main(String[] args) {
                // TODO Auto-generated method stub

                String str="asdqweertwsdfsdsd";
                if(panduan(str)){
                        System.out.println("shi");
                }
                else
                        System.out.println("bushi");
        }
        private static boolean panduan(String str) {
                // TODO Auto-generated method stub
                char[]c=str.toCharArray();
                for(int i=0;i<c.length/2;i++){
                        if(c==c[c.length-i-1])
                                return true;
                        
                }
                return false;
        }

}



作者: 郑飞    时间: 2014-10-7 11:25
没运行 就看到两个地方
char[]c 这里怎么连到一起了
i<=c.length/2

作者: Fightin黑马    时间: 2014-10-7 16:15
for(int i=0;i<c.length/2;i++){
                        if(c==c[c.length-i-1])
                                return true;
}
这个地方只要有一个地方相同就会打印是,return应该改成continue 等到所有的都判断完是TRUE 才返回TRUE 只要有一个是false结果就是FALSE
  1. private static boolean panduan(String str) {
  2.                 boolean bool = true;
  3.                 char[] c = str.toCharArray();
  4.                 for (int i = 0; i < c.length / 2; i++) {
  5.                         if (c[i] == c[c.length - i - 1]) {
  6.                                 continue;
  7.                         }
  8.                         bool = false;
  9.                 }
  10.                 return bool;
复制代码





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