While循环解决加性持久性

问题描述

while循环的第一次迭代之后,num的值变为29,其长度为2(您可以通过取消注释打印内容并在for循环之后中断来查看此内容)。然后while循环应该再次运行,因为num的长度为2,但是实际上它变成了无限循环。我想念的是什么?

public static void main(String[] args)
{
    String num = "9992";
    
    calculateAdditivePersistance(num);
}

public static void calculateAdditivePersistance( String num )
{
    int count   = 0;
    int total   = 0;
    
    while(num.length() > 1)
    {
        for( int x = 0; x < num.length(); x++)
        {
            total+= Character.getNumericValue(num.charAt(x));
        }
        num = Integer.toString(total);
        //System.out.println(num); uncomment this out to see that num value becomes 29
        count++;
        //break;  uncomment this out to see that num value becomes 29
    }
    System.out.println(count);
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)