public class demo01 {
public static boolean flag=true;
public static void printStr(String str)
{
System.out.println("str.length():"+str.length());
char chr[]=new char[str.length()];
for (int i = 0; i < str.length(); i++) {
chr[str.length()-i-1]=str.charAt(i);
                          //逆序输出结果
System.out.println(chr);
}

for (int i = 0; i < str.length(); i++) {

System.out.println(chr[i]+"   "+str.charAt(i));
                          //比较两者间数字是否相等
if(chr[i]==str.charAt(i))
{
flag=true;
System.out.println(flag);
}
else
{
flag=false;
}
}
if(flag==true)
{
System.out.println("是回文数");
}
else
System.out.println("不是回文数。。");
}
public static void main(String[] args) {
printStr("128321");
}
}
   这段代码打印的结果是  是回文数控制台输出结果:
 123821
1   1
true
2   2
true
3   8
8   3
2   2
true
1   1
true
是回文数

解决方案 »

  1.   


    public class demo01 { 
    public static boolean flag=true; 
    public static void printStr(String str) { 
    System.out.println("str.length():"+str.length()); 
    char chr[]=new char[str.length()]; 
    for (int i = 0; i < str.length(); i++) { 
    chr[str.length()-i-1]=str.charAt(i); 
                              //逆序输出结果 
    System.out.println(chr); 
    }  for (int i = 0; i < str.length(); i++) {  System.out.println(chr[i]+"  "+str.charAt(i)); 
                              //比较两者间数字是否相等 
    if(chr[i]==str.charAt(i)) { 
    flag=true; 
    System.out.println(flag); 

    else { 
    flag=false;
    break;



    if(flag==true) { 
    System.out.println("是回文数"); 

    else 
    System.out.println("不是回文数。。"); 


    public static void main(String[] args) { 
    printStr("128321"); 


    发现有不相同的要break啊,停止比较,而不是一直比较完