Java 遍历数字中的每个数字

Java 遍历数字中的每个数字,java,math,numbers,iteration,Java,Math,Numbers,Iteration,我正在尝试创建一个程序,该程序将告诉给定给它的数字是否为“”。找到一个快乐的数字需要对数字中的每个数字进行平方运算,并将每个数字的平方结果相加 在Python中,可以使用如下内容: SQUARE[d] for d in str(n) 但在Java中,我找不到如何遍历数字中的每个数字。正如您所知,我是新手,在Java文档中找不到答案。您可以使用模10运算得到最右边的数字,然后将数字除以10得到下一个数字 long addSquaresOfDigits(int number) { long

我正在尝试创建一个程序,该程序将告诉给定给它的数字是否为“”。找到一个快乐的数字需要对数字中的每个数字进行平方运算,并将每个数字的平方结果相加

在Python中,可以使用如下内容:

SQUARE[d] for d in str(n)

但在Java中,我找不到如何遍历数字中的每个数字。正如您所知,我是新手,在Java文档中找不到答案。

您可以使用模10运算得到最右边的数字,然后将数字除以10得到下一个数字

long addSquaresOfDigits(int number) {
    long result = 0;
    int tmp = 0;
    while(number > 0) {
        tmp = number % 10;
        result += tmp * tmp;
        number /= 10;
    }
    return result;
}

你也可以把它放在一个字符串中,把它转换成一个char数组,然后像
Math.pow(charArray[i]-'0',2.0)那样迭代它

假设数字以整数开头:

int num = 56;
String strNum = "" + num;
int strLength = strNum.length();
int sum = 0;

for (int i = 0; i < strLength; ++i) {
  int digit = Integer.parseInt(strNum.charAt(i));
  sum += (digit * digit);
}
int num=56;
字符串strNum=”“+num;
int strLength=strNum.length();
整数和=0;
对于(int i=0;i
我想知道在Java中,哪种方法能最快地将正数拆分为数字,字符串还是模

  public static ArrayList<Integer> splitViaString(long number) {

    ArrayList<Integer> result = new ArrayList<>();
    String s = Long.toString(number);

    for (int i = 0; i < s.length(); i++) {
      result.add(s.charAt(i) - '0');
    }
    return result; // MSD at start of list
  }
公共静态ArrayList splitViaString(长数字){
ArrayList结果=新建ArrayList();
字符串s=Long.toString(数字);
对于(int i=0;i
vs

publicstaticarraylistsplitviaomodulo(长数字){
ArrayList结果=新建ArrayList();
而(数量>0){
整数位数=(整数)(数字%10);
结果。添加(数字);
数目/=10;
}
返回结果;//列表开头的LSD
}
通过传递
Long.MAX_值
10000000次来测试每个方法,字符串版本耗时2.090秒,模版本耗时2.334秒。(在Eclipse Neon中运行的64位Ubuntu上的Oracle Java 8)


因此,实际上并不是很多,但我有点惊讶,在上面的示例中,我们可以使用字符串更快

int digit = Character.getNumericValue(strNum.charAt(i));
而不是

int digit = Integer.parseInt(strNum.charAt(i));

此代码返回符合您描述的第一个数字(1之后)

public static void main(String[] args) {
    int i=2;
    // starting the search at 2, since 1 is also a happy number
    while(true) {
        int sum=0;
        for(char ch:(i+"").toCharArray()) { // casting to string and looping through the characters.
            int j=Character.getNumericValue(ch);
            // getting the numeric value of the current char.
            sum+=Math.pow(j, j);
            // adding the current digit raised to the power of itself to the sum.
        }
        if(sum==i) {
            // if the sum is equal to the initial number
            // we have found a number that fits and exit.
            System.out.println("found: "+i);
            break;
        }
        // otherwise we keep on searching
        i++;
    }
}

@艾萨克·刘易斯:添加了一个(未经测试的)代码,我认为它可以让你们得到每个数字的平方的结果。你们需要修正“数字/10”来分配某处。啊,请不要用Math.pow来计算一个数字的平方<代码>结果+=((数字%10)*(数字%10))
会更好(或者使用额外的变量来节省双精度
%
)。我认为你的
number/10
应该是
number/=10
。Darron和Paulo观察得很好。如果你回答完这个问题,请接受答案。复习:什么例子?请你修改一下你的问题,再精确一点好吗?答案的顺序可能会因他们的投票而改变。虽然这段代码可能会解决这个问题,但如何以及为什么解决这个问题将真正有助于提高您的帖子质量,并可能导致更多的投票。请记住,你是在将来回答读者的问题,而不仅仅是现在提问的人。请在回答中添加解释,并说明适用的限制和假设。对不起,这是我的第一个答案
public static void main(String[] args) {
    int i=2;
    // starting the search at 2, since 1 is also a happy number
    while(true) {
        int sum=0;
        for(char ch:(i+"").toCharArray()) { // casting to string and looping through the characters.
            int j=Character.getNumericValue(ch);
            // getting the numeric value of the current char.
            sum+=Math.pow(j, j);
            // adding the current digit raised to the power of itself to the sum.
        }
        if(sum==i) {
            // if the sum is equal to the initial number
            // we have found a number that fits and exit.
            System.out.println("found: "+i);
            break;
        }
        // otherwise we keep on searching
        i++;
    }
}