首页 > 其他 > 详细

整数换为字符串

时间:2015-03-23 09:41:52      阅读:234      评论:0      收藏:0      [点我收藏+]

怎么样把一个整数转换为字符串,而不借助任何的函数库?

问题分析

通过对问题的分析,我们知道要达到目的,我们的算法过程描述如下:

  1. 拆分整数
  2. 把拆分的各个数字换为字符,放入字符数组
  3. 刚刚转换的字符串是逆序的,我们需要反转字符串

代码实现如下:

/**
 * 把整数转换为字符串
 * @author Administrator
 *
 */
public class CharToString {

    public static String change(int num){
        //分割整形数据
        char temp = ‘0‘;
        char [] bf = new char[10];
        int i = 0;
        while(num!=0){
            temp = (char) ((num%10)+‘0‘);
            bf[i] = temp;
            num = num/10;
            i++;
        }

        char buf[] = new char[bf.length];
        int t = i;
        for(int j=0;j<t;j++){
            buf[j] = bf[i];
            i--;
        }
        return new String(buf);
    }

    public static void main(String[] args) {

        System.out.println(change(1234));
    }

}

整数换为字符串

原文:http://blog.csdn.net/github_20066005/article/details/44539351

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!