判断字符串回文

2023-04-16 14:38:08 浏览数 (1)

判断字符串回文 /**

  • String常用方法:
  • a.equals(b) 重写后比较值 重写前继承父类Object类的该方法比较地址值(见源码)
  • charAt() 返回索引指定处字符
  • a.compare(b)
  • replace(char new ,char old) 用新字符替代旧字符
  • toLowCase()将字符串中所有的字符全部转换为小写
  • toUpperCase()将字符串中所有字符全部转换为大写 */
代码语言:javascript复制
boolean judge =false;
        BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(System.in));
        try {
            System.out.print("请输入一串字符串:");
            String string=bufferedReader.readLine();
            for (int i = 0; i < string.length(); i  ) {
                Character character=string.charAt(i);
                Character character1=string.charAt(string.length()-1-i);
                if (character.equals(character1)){
                    judge=true;
//                    continue用法: 跳出当前次循环 进行i   然后直接进入下一波循环
                    continue;
                }else {
                    judge=false;
                    System.out.println("不是回文!");
                    break;
                }
            }
            if (judge){
                System.out.println("是回文!");
            }
        }catch (IOException e){
            System.out.println("io 异常 !");
        }

0 人点赞