java编程,求程序 1. 判断一个字符串是否是一个回文字符串.回文字符串正序读和倒序读结果都相同.

2024-11-29 13:49:29
推荐回答(2个)
回答1:


import java.util.Scanner;
public class TestP{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("请输入字符串");
String s=sc.next();
char[] ch=s.toCharArray();
for(int i=0;i if(i==ch.length-1){
System.out.println("是回文字符串");
}else if(ch[i]==ch[ch.length-i-1]){
continue;
}else{
System.out.println("不是回文字符串");
break;
}
}
}
}

回答2:

public class test {
public void fun(String str){
Integer length = str.length();
int flag = 0;
if(length != 0 && length%2 == 0){
char[] c = str.toCharArray();
for(int i = 0 ; i < length/2 ; i++){
int i2 = length-i-1;
System.out.println(i+"----"+i2);
if(c[i] != c[length-i-1]){
++flag;
System.out.println("不是回文");
break;
}
}
if(flag == 0){
System.out.println("是回文");
}
}else{
System.out.println("不是回文,回文字符数必须为偶数个");
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new test().fun("abcdeedcba");
}

}