《The Java Tutorial_3ed》中的一道练习题?求解

saidas 2006-09-30 11:32:37
2、What two Integer methods would you use to convert a string expressed in base 5 into the equivalent int? For example, how would you convert the string "230" into the integer value 65? Show the code you would use to accomplish this task.

因为英语不过关,不知道题中的“ in base 5 ”是何意?
这道题目在第三版的原书P164.Q1.b
...全文
249 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
saidas 2006-10-01
  • 打赏
  • 举报
回复


谢谢 szVista() ——我刚开始认为是五进制数,尔后感觉不对,因为我也觉
得与250[5] 对应的值不是65.又仔细演算了一下 230[5]==65[10]
2*(5^2)+3*5+0 == 65..这样看来,in base 5 确为以5为基数的意思。
谢谢 gefengxztg(戈峰)
—— 仔细读了您的代码,对一个初学者来说,真是受益菲浅。

===============
因为这个小问题烦了快一周了,没想到经人轻轻一点,豁然开朗。
参考了你们的回复,我觉得原文可译为:

2、使用哪两个方法能将一个以5进制数来表示的串转化为与之相应的整形数据(十进制)?
请各位朋友指点。

gefengxztg 2006-10-01
  • 打赏
  • 举报
回复
楼上先解决了,呵呵!!
szVista 2006-10-01
  • 打赏
  • 举报
回复
/*
* 将字符串数字转换成以5为基数的五进制数
* @author diego.liu
* @date 2006-09-30
*/

public class String2Base5 {

/**
* @param args
*/
public static void main(String[] args) {
if(args.length != 1){
System.out.println("pls input a String.");
return;
}
String test = args[0];//获取命令行参数
char[] chars = test.toCharArray();//转换成字符数组
int len = chars.length;//字符数组长度
int result = 0;//计算结果
for (int i = 0; i < len; i++) {
int temp = Integer.parseInt(String.valueOf(chars[i]));
double bb = Math.pow(5,len-1-i)*temp;//将每位转换成5进制数字
result += bb;//相加
}
System.out.println("the result is:" + result);
}

}
gefengxztg 2006-10-01
  • 打赏
  • 举报
回复
但好像不对哦
"230" into the integer value 65?
怎么是65 5进制没6 5啊,打错了不啊,楼主
gefengxztg 2006-10-01
  • 打赏
  • 举报
回复
呵呵
错了,是以5为基数的意思,即5进制数
gefengxztg 2006-09-30
  • 打赏
  • 举报
回复
应该是5个字符以内的意思哦
日总是我哥 2006-09-30
  • 打赏
  • 举报
回复
sf~

23,404

社区成员

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

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