天天看点

判断字符对称性

判断一个字符串是否是对称字符串,例如"abc"不是对称字符串,"aba"、"abba"、"aaa"、"mnanm"是对称字符串。是的话输出”Yes”,否则输出”No”。

注意:使用循环和判断语句实现。

package second;

import java.util.Scanner;

public class 判断对称性 {
    public static void main(String[] args) {
        System.out.println("请输入一个字符串:");
        Scanner sc =new Scanner(System.in);

        String st= sc.next();

        boolean result = true;

        int l=st.length();

        int head = 0;
        int tail = l-1;

        int count = (head+tail)/2;
        for (int i = 0; i < count; i++) {
            if(st.charAt(head++) != st.charAt(tail++)) {
                result = false;
            }
        }

        if(result)
        {
            System.out.println("Yes");
        }else{
            System.out.println("No");
        }
    }
}
      

  

路是自己的,没必要拿别人的标准衡量自己,它只配成为垫脚石。