can anyone give me an example for the answer of this question?

kennyl 2004-12-15 03:24:20
Write a method reverse that takes an integer value and returns the number with its digits reversed. For example, given the number 76030, the method should return 3067.
...全文
78 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
kennyl 2004-12-15
  • 打赏
  • 举报
回复
to: zhaohao
写一个方法reverse,接收一整数值,返回它的相反数值。如:输入整数76030,方法就返回3067。
zhaohao19853 2004-12-15
  • 打赏
  • 举报
回复
英文不好,请楼主发一个中文的,让小弟我做做,谢谢了
kennyl 2004-12-15
  • 打赏
  • 举报
回复
/* this is my answer. how do you think? */
static int reverse(int n)
{
String numString = String.valueOf(n);
int a = numString.length();
int num = 0;
for(int i=1, j=(int)Math.pow(10, a-1); i<=a; i++, j/=10)
{
num += n%10*j;
n/=10;
}
return num;
}
jackkui 2004-12-15
  • 打赏
  • 举报
回复
The method reverse() StringBuffer can satify you.
StringBuffer number = new StringBuffer("123450");
StringBuffer reverseNubmer = number.reverse();
这样就行了吧,如果你要去掉0的话使用String的方法找到第一个非0所在位置
然后用substring(index);就行了。
yulchina 2004-12-15
  • 打赏
  • 举报
回复
int reverse(int i)
{
int result = i;
String num = String.valueOf(i);
byte[] arrayNum = num.getBytes();
int arrlength = arrayNum.length;
byte[] newArray = new byte[arrlength];
for(int j = arrlength - 1,k = 0; j >= 0; j--,k++)
{
newArray[k] = arrayNum[j];
}
String newNum = new String(newArray);
try
{
result = Integer.parseInt(newNum);
} catch(Exception e){}
finnally
{
return result;
}
}
thomas_20 2004-12-15
  • 打赏
  • 举报
回复
稍加修改:
private String getRs(){
String number = "1000";
String[] n = number.split("");
StringBuffer result = new StringBuffer();

for (int j=n.length-1;j>=0;j--){
result.append(n[j]);
}
while(result.substring(0,1).equals("0")&&result.length()>1){
result.delete(0,1);
}
System.out.println(result);
return result.toString();
}
thomas_20 2004-12-15
  • 打赏
  • 举报
回复
private String getRs(){
String number = "76030";
String[] n = number.split("");
StringBuffer result = new StringBuffer();

for (int j=n.length-1;j>=0;j--){
result.append(n[j]);
}
while(result.substring(0,1).equals("0")){
result.delete(0,1);
}
System.out.println(result);
return result.toString();
}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧