重新写一个函数,把一个10进制整数的字符串转换成数字

sshany 2001-07-19 01:40:24
要求重新写一个函数,但不能用已有的函数
怎样写呀??????????????????????????
我全部分都给上啦
...全文
125 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
masterz 2001-07-19
  • 打赏
  • 举报
回复
number = 个位数*1+ 10位数*10 +百位数*100+...
String data;
个位数=data.charAt(data.length()-1);
10位数=data.charAt(data.length()-1);
...位数=data.charAt(0);

正常情况下data.charAt(n)应该 大于等于'0',小于等于'9',否则不是正确的数字
sshany 2001-07-19
  • 打赏
  • 举报
回复
已经给分啦,能否简单解释一下,谢谢
masterz 2001-07-19
  • 打赏
  • 举报
回复
int string2Int(String s) throws NumberFormatException
{
if(s==null)
throw new NumberFormatException();
int index=1;
int result=0;
int len=s.length();
char zero='0';
for(int i=len;i>0;i--)
{
char c=s.charAt(i-1);
int digit=c-zero;
if(digit<0||digit>9)
{
throw new NumberFormatException();

}
result = result+ digit*index;
index = index*10;
}
return result;
}
sshany 2001-07-19
  • 打赏
  • 举报
回复
例如 String num="12345";
bobosji 2001-07-19
  • 打赏
  • 举报
回复
就是把数字字符串转化为数字?
是什么样的字符串啊,长度有多少。。。。。??

62,612

社区成员

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

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