---------------------- 黑马程序员 Android培训、期待与您交流! ----------------------
public class Test02 {
/**
* @param args
*/
public static void main(String[] args) {
String str = null;
System.out.println("“" +str + "”" + isSymmetric(str));
str = "";
System.out.println("“" +str + "”" + isSymmetric(str));
// 偶数长度
str = "abccba";
System.out.println("“" +str + "”" + isSymmetric(str));
str = "abcdba";
System.out.println("“" +str + "”" + isSymmetric(str));
// 奇数长度
str = "abcdcba";
System.out.println("“" +str + "”" + isSymmetric(str));
str = "abca";
System.out.println("“" +str + "”" + isSymmetric(str));
}
public static String isSymmetric(String str) {
if (null == str) {
return "字符串为空";
}
for (int i = 0; i < str.length() / 2; i++) {
// 比较距字符串两头长度相同的字符是否一样
if (str.charAt(i) != str.charAt(str.length() - i - 1)) {
return "不是对称字符串";
}
}
return "是对称字符串";
}
}
---------------------- 黑马程序员 Android培训、期待与您交流! ----------------------