简体   繁体   English

我应该如何反复循环 Java 中的“函数”?

[英]How am I supposed to loop repeatedly over a "function" in Java?

I have written a code which takes in a number and multiplies it's digits together and here it is.我写了一个代码,它接受一个数字并将它的数字相乘,就在这里。

public class Happy
    {   public static int findSum(int k)
        {
            int sum = 0;
            while (k != 0)
            {
                sum = sum + ((k % 10)*(k % 10));
                k = k/10;
            }
            return sum;
        }
        public static void main(String args[])
        {
            System.out.println(findSum(713));
        }
    }

and my code returns 19 when my input is 82, because 1^2 + 9^2 is 82.当我的输入为 82 时,我的代码返回 19,因为 1^2 + 9^2 是 82。

However, I want to modify my code so that my code would continue to square each digit and add them together until there is only one non-zero digit left .但是,我想修改我的代码,以便我的代码继续对每个数字求平方并将它们相加,直到只剩下一个非零数字

So basically, if my input is 19, the code would return 1 because:所以基本上,如果我的输入是 19,代码将返回 1 ,因为:

1^2 + 9^2 = 82, from which digits 8 and 2 would do: 8^2 + 2^2 = 68, from which digits 6^2 + 8^2 = 100, from which 1^2 + 0^2 + 0^2 = 1. 1^2 + 9^2 = 82,其中数字 8 和 2 可以:8^2 + 2^2 = 68,其中数字 6^2 + 8^2 = 100,其中 1^2 + 0^ 2 + 0^2 = 1。

What changes to my code should I make in order for it to work?我应该对我的代码进行哪些更改才能使其正常工作?

You could call your method recursivly by checking if sum > 9您可以通过检查sum > 9来递归调用您的方法

public static int findSum(int k)
{
    int sum = 0;
    while (k != 0)
    {
        sum = sum + ((k % 10)*(k % 10));
        k = k/10;
    }
    return sum > 9 ? findSum(sum) : sum;
}
    public static boolean check_base10(int k){
            String total = String.valueOf(k);
            for (int i = 1; i < total.length(); i++) {
                if (total.charAt(i) != '0' ) {
                    return false;
                }
            }
            return true;
        }
    public static void main(String args[])
        {
            int result = findSum(713) ;
            do { /* rerun the function until the condition of while is verified*/
                result = findSum(result) ;
               
            }
            while(!check_base10(result)) ; /* check wether number is base 10 */
            System.out.println(result);
        }

I think this should answer your question, you rerun your function until your condition is successfully met in the while()我认为这应该回答你的问题,你重新运行你的 function 直到你的条件在while()中成功满足

Only start accumulating after encountering the first non-zero remainder:只有在遇到第一个非零余数后才开始累加:

public static int findSum(int k) {
    int sum = 0;
    boolean foundNonZero = false:
    for (; k != 0; k /= 10) {
        if (foundNonZero) {
            sum += (k % 10)*(k % 10);
        } else {
            foundNonZero = k % 10 != 0;
        }
    }
    return sum;
}

Note the brevity achieved by the replacement of while with for and use of terse arithmetic syntax.请注意用for替换while和使用简洁的算术语法所达到的简洁性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 给定必须在Java中复制的次数,我应该如何写一个数组 - How am I supposed to write over an array given the number of times it has to be copied in Java Java:我如何每个星期天的上午12:01重复运行功能? - Java: How do I run function repeatedly every week Sunday at 12:01 AM? 我应该如何从Java Applet绘制图像? - How am I supposed to draw an image from my Java Applet? 我应该如何将此Spark Scala行转换为Java - How I am supposed to translate this spark scala line to java 我的循环错了。 Java新手。 对我要去哪里错有任何见解吗? 我应该算硬币 - My loop is wrong. Brand new to Java. Any insight as to where I am going wrong? I am supposed to calculate coins 当我在 for 循环内迭代数组元素时,如何迭代 Java 中的数组元素? - How do I iterate over elements of an array in Java while I am inside of a for loop to iterate over the elements of an array? 由于我不想超过 100,我应该如何限制最多 100? JAVA - How am I supposed to limit up to 100 since I do not want to exceed it to 100? JAVA 我应该如何将 100 行代码减少到更少? (Java机器人) - How am I supposed to reduce 100 lines code to fewer? (Java robot) Java:我应该如何关闭包裹在SLF4J门面中的异步附加程序? - Java: How am I supposed to close async appender wrapped in SLF4J facade? 如何遍历Java参数? - How do I loop over arguments java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM