如何对这个数字进行数组,而不是将它转换为字符串 [英] how to to array this numbers w/o converting it too string

查看:61
本文介绍了如何对这个数字进行数组,而不是将它转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数返回整数我想像这样计算

的例子。 put值为512,计算结果为5 + 1 + 2 = 8。有可能将它转换为整数数组,并将其转换为字符串吗?

I have a function that return integer i would like to compute it like this
example. The value put is 512 the computation would be 5+1+2=8. It is possible to Convert that to an array of integer withought converting it to string?

推荐答案





看看这里:

http://stackoverflow.com/a/4580343 [< a href =http://stackoverflow.com/a/4580343target =_ blanktitle =New Window> ^ ]


int Pattern(int number)
{
    int digit = 0;
    int total =0;
    do
    { 
        digit = number % 10;
        number = number / 10;
        total += digit;
    }
    while (number > 0);
    return total;

}


这篇关于如何对这个数字进行数组,而不是将它转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆