当前位置: 代码迷 >> Java相关 >> 求回文数
  详细解决方案

求回文数

热度:334   发布时间:2006-05-14 17:21:00.0
求回文数

import java.io.*;
import java.util.*;
public class huiwen{
public static void main(String args[]){
int i,j,k,a;
String x,y;
int shuzhu[];
int shuzhu1[];
shuzhu=new int[100];
shuzhu1=new int[100];
System.out.println("\nplease input some strings:");
String s=" ";
try
{
BufferedReader in =
new BufferedReader(new InputStreamReader(System.in));
s = in.readLine();
}catch(IOException e){}
a=s.length();
if(a%2==0)
{

for(i=0;i<(a/2);i++)
shuzhu[i]=s.charAt(i);
x=shuzhu.toString();
for(j=a-1;j>(a-1)/2;j--)
shuzhu1[j]=s.charAt(j);
y=shuzhu1.toString();
if(x.compareTo(y)==0)
System.out.println("\nthe strings"+s+"is an huiwenshu!\n");
}

}
}
这个程序没有错!但是就是没有结果,各位高手帮我看一下好吗?麻烦拉!

搜索更多相关的解决方案: 回文  

----------------解决方案--------------------------------------------------------
你这个程序当然没有输出,如果长度为偶数,就不执行if语句;如果为奇数,shuzhu[]就为空.你要得到什么功能?看不懂!
----------------解决方案--------------------------------------------------------
都不知道楼主要什么样的结果
----------------解决方案--------------------------------------------------------
import java.io.*;
import java.util.*;
public class huiwen {
public static void main(String args[]){
int b,a;
System.out.println("\nplease input some strings:");
String s=null;
try
{

BufferedReader in =
new BufferedReader(new InputStreamReader(System.in));
s = in.readLine();
}catch(IOException e){}

a=s.length()-1;
b=0;
while(b<a){
if(s.charAt(a)==s.charAt(b)){
a--;
b++;

}
else
break;

}
if(b<a)
System.out.println(" 不是回文数");
else
System.out.println("是回文数 ");

}
}
//用java 的字符串函数很容易就实现了

[此贴子已经被作者于2006-5-14 23:24:18编辑过]


----------------解决方案--------------------------------------------------------
用JAVA里的Stack解决这个问题 最方便
----------------解决方案--------------------------------------------------------
这样不是简单些吗,
import java.io.*;
public class huiwen
{
public static void main(String args[])throws IOException
{ String s=new String();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("insert the string:");
s=br.readLine();
StringBuffer s1=new StringBuffer(s);
s1.reverse();
System.out.println(s1);
int n=s1.length();
char a[]=new char[n];
s1.getChars(0,n,a,0);
String s2=new String(a);
if(s.equals(s2))
System.out.println("此字符串是回文");
else System.out.println("此字符串不是回文");
}
}
----------------解决方案--------------------------------------------------------

楼上的方法更简单,不错.值得学习


----------------解决方案--------------------------------------------------------
非常感谢大家啊!在你们的帮助下我已经解决问题了!
----------------解决方案--------------------------------------------------------
  相关解决方案