简体   繁体   English

如何迭代地计算一个数字的总和 - java / bluej

[英]How to calculate the sum of digits in a number iteratively - java/bluej

I am creating two methods - one that calculates the sum of the digits in a number recursively, and the other iteratively. 我正在创建两个方法 - 一个以递归方式计算数字中的数字之和,另一个以迭代方式计算。

I have created the recursive method, and for the most part I understand the concept of finding the sum of digits, but I am not sure how to correctly put it into an iterative method. 我已经创建了递归方法,并且在很大程度上我理解了找到数字之和的概念,但我不确定如何正确地将它放入迭代方法中。 My code does not give me the correct output. 我的代码没有给我正确的输出。

  public static int iterativeDigitSum(long n) {
        if(n < 0){ 
           String err = "n must be positive. n = " + n;
         throw new IllegalArgumentException(err);
       }
     if(n == 0){
           return 0;
       }

        long sum = 0;
        long i = 0;
     while(n > 0){
             i = n % 10;
             sum = sum + n;
             n = n / 10;
       }
       int inSum = (int)sum;
       return inSum;
}

The number "n" is 10, meaning the expected output is 1. I am getting 11. Could you please explain to me what I am doing wrong, and possibly how to fix it? 数字“n”是10,意味着预期的输出是1.我得到11.你能告诉我我做错了什么,可能还有如何修复它? Thank you so much. 非常感谢。

Basically, the algorithm consists of three steps: 基本上,该算法包括三个步骤:

  1. Get the rightmost digit of the number. 获取该数字的最右边数字。 Since each digit in a number has a rank of units, tens, hundreds, thousands etc based on its position the rightmost digit is the remainder of dividing the number by 10: 由于数字中的每个数字都有一个单位的等级,数十,数百,数千等基于其位置,最右边的数字是数字除以10 的余数:

    digit = n % 10

  2. Sum the digit up: 数字总和:

    sum += digit

  3. Move all the digits one place to the right by dividing the number by 10. The number becomes 10 times smaller: 将数字除以10,将所有数字向右移动一位。数字变小10倍:

    n = n / 10

    Effectively, this will "provide" the next rightmost digit for step 1. 实际上,这将“提供”步骤1的下一个最右边的数字。

The above three steps are repeated until the value of number becomes zero. 重复上述三个步骤,直到数值变为零。

You can help yourself visualize the above explanation by adding some "debugging" information into your code: 您可以通过在代码中添加一些“调试”信息来帮助自己查看上述说明:

public static int iterativeDigitSum(long n)
{
    long sum = 0;
    int i = 1;
    System.out.println("i\tn\tdigit\tsum");        
    while(n > 0) {
        long digit = n % 10;
        sum += digit;            
        System.out.println(i + "\t" + n + "\t" + digit + "\t" + sum);
        n = n / 10;
        i++;
    }
    System.out.println("\t" + n + "\t\t" + sum);
    return (int)sum;
}

Please note that the i variable is used to count the loop iterations and the digit variable holds the rightmost digit of the number in each iteration. 请注意, i变量用于计算循环迭代,而digit变量保存每次迭代中最右边的数字。

Given the number 10, the output to the BlueJ console is: 给定数字10,BlueJ控制台的输出是:

i   n     digit   sum
1   10    0       0
2   1     1       1
    0             1

and for the number 2019: 而对于2019年:

i   n       digit   sum
1   2019    9       9
2   201     1       10
3   20      0       10
4   2       2       12
    0               12

Hope it helps. 希望能帮助到你。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM