一下是我写的代码,运行有错误:
public static void main(String[] args) throws IOException {
String str;
int i,j;
String[] huiwen=new String[20];
System.out.print("请输入需要判断的文字:");
BufferedReader buf=new BufferedReader(new InputStreamReader(System.in));
str=buf.readLine();
huiwen[20]=str;
for(i=0,j=huiwen.length;i<j;i++){
if(huiwen[i]==huiwen[j]){
j--;
if(i==j){
System.out.print("是回文");
}
}else{
System.out.print("不是回文");
}
}

}
请问有哪位朋友知道Java判断回文代码,能回复出来讲解一下吗?     本人坐等,学习。  先谢谢了。javastring回文

解决方案 »

  1.   

    huiwen[20]=str;数组越界,你定义数组长度20,数组下表0-19
      

  2.   

    package org.love.yan.xing;import java.io.*;
    public class HuiWen{
        public static void main(String[] args)throws IOException{
            String s;
            System.out.print("请输入需要判断的文字:");
            BufferedReader buf=new BufferedReader(new InputStreamReader(System.in));
            s=buf.readLine();
            if(comp(s))
                System.out.println(s+"  是回文");
            else
                System.out.println(s+"  不是回文");
            }
        public static boolean comp(String s){
            int i=0;
            char[] charArray=s.toCharArray();
            while(i<s.length()/2){
               if(charArray[i]!= charArray[s.length()-1-i])
                  return false;
               i++;
              }
            return true;
          }
    }
      

  3.   

    public static void main(String[] args) {
    String str;
    int i, j;
    System.out.print("请输入需要判断的文字:");
    BufferedReader buf = new BufferedReader(
    new InputStreamReader(System.in));
    try {
    str =buf.readLine();
    String s[]=new String[]{str};
    for (i = 0, j = s.length; i < j; i++) {
    System.out.println(s[i]+"===="+s[j-1]);
    if (s[i] == s[j-1]) {
    j--;
    if (i == j) {
    System.out.print("是回文");
    }
    } else {
    System.out.print("不是回文");
    }
    }
    } catch (Exception e) {
    e.printStackTrace();
    } }
      

  4.   

    return new StringBuilder(str).reverse().toString().equals(str);
      

  5.   

    yanxing下面那段代码没弄懂:
    public static boolean comp(String s){
            int i=0;
            char[] charArray=s.toCharArray();
            while(i<s.length()/2){
               if(charArray[i]!= charArray[s.length()-1-i])
                  return false;
               i++;
              }
            return true;
          }char[] charArray=s.toCharArray();  中  s.toCharArray();  可以这样定义数组吗?