java中写一个递归函数,输入一个整数,反序输出这个整数。在线 求答案 ,谢谢!

java中写一个递归函数,输入一个整数,反序输出这个整数。在线 求答案 ,谢谢!

第1个回答  推荐于2017-09-16
这个需要递归吗?把整数转成字符串,然后转成字符数组,然后声明一个新的数组,反序写进去就好了,为什么要递归?
String string=i+"";
char[] c=string.toCharArray();
char[] c2=new char[c.length];
for (int j = 0; j < c2.length; j++) {
c2[j]=c[c.length-j-1];
}
return Integer.parseInt(new String(c2));
借用下冰羽的递归实现
public static int test(int i) {
String temp="";
if (i>10) {
temp+=test(i/10)+"";
}
return Integer.parseInt((i%10)+temp);
}本回答被提问者和网友采纳
第2个回答  2013-01-20
package test;
public class Q3 {
public static void main(String[] args){
System.out.println(test("12345"));

}

public static String test(String a){
String end=a.charAt(a.length()-1)+"";
if(a.length()==1){
return a;
}
return end+test(a.substring(0,a.length()-1));
}
}

按你的意思用递归,如果一定要整数,先把整数转换成字符串就行了,一定用整数写也可以,不过字符串没有长度限制,比整数要好点
第3个回答  2013-01-20
用字符串很简单。
刚刚稍改了一下,如下应该能满足你要求:
public class T {
public static int test(int i){
int d=(int) ((i%10)*Math.pow(10,(i+"").length()-1));
if(i/10!=0){
d=d+test(i/10);
}
return d;
}
public static void main(String[]args){
System.out.println(test(1234));
}
}追问

这方法 挺好的, 可是 .....

相似回答