java 将数字相加为整数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28155746/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 13:05:51  来源:igfitidea点击:

Sum the Digit in an integer

java

提问by cselover

* Write a programe that reads an integer between 0 and 1000 and adds all the 
 * digits in the integer. For example, if an integer is 932 the sum of all its
 * digits is 14.
 */
package sumthedigitsinaninteger;

/**
 *
*/
import java.util.Scanner;

public class SumTheDigitsInAnInteger {

public static void main(String[] args) 
{
   Scanner input = new Scanner(System.in);   

   System.out.print(" Enter a number between 0 and 1000 : ");

   int  num = input.nextInt();

   int extract = num % 10;

   int remove_extract = extract / 10 ;

   int answer = extract + remove_extract;

   System.out.print(" The sum of the digit is " + answer);

}    
}

I am so confused about this problem. I am reading a chapter in java book called Introduction to Java Programming 9th edition by Y.Daniel Liang. This chapter is a bout elementary programing which only teach me how to declare basic variable and do basic arithmetic. When I check online for help most of help I got with for loop or while loop which I have not read it yet. Anyone know how to do this problem without for loop or while loop because I have not get to that chapter yet. I know % means a reminder of division and / means quotient. when I did this problem manual I got right answer for 932 which is 14. I got it by doing a division twice by 10 and add a reminder of those two division which was a 3 and 2 then I add a motioned a 9 to 3+2 which gave me a 14. I am so confused how convert a this logic into source code with my little knowledge of loops. Thanks.

我对这个问题很困惑。我正在阅读 Y.Daniel Liang 编写的 Java 编程简介第 9 版中的一章。本章是初级编程,只教我如何声明基本变量和做基本算术。当我在网上查询帮助时,我得到的大部分帮助都是通过 for 循环或 while 循环获得的,但我还没有阅读。任何人都知道如何在没有 for 循环或 while 循环的情况下解决这个问题,因为我还没有读到那一章。我知道 % 表示提醒除法和 / 表示商。当我做这个问题手册时,我得到了 932 的正确答案,即 14。我通过除以 10 两次得到它,并添加了这两个除法的提醒,即 3 和 2,然后我添加了一个 9 到 3+ 2 这给了我 14。我很困惑如何将这个逻辑转换为源代码,而我对循环知之甚少。

回答by MinecraftShamrock

You can do this without any loops if you know that the maximum is 1000.

如果您知道最大值为 ,则无需任何循环即可执行此操作1000

This would work with:

这将适用于:

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);   
    System.out.print(" Enter a number between 0 and 1000 : ");
    int num = input.nextInt();

    int ones = num % 10;

    int tens = (num / 10) % 10 ;

    int houndreds = (num / 100) % 10;

    int answer = ones + tens + houndreds;

    System.out.print(" The sum of the digit is " + answer);

}

This works because you know the number is between0 and 1000. But as soon as you have learned about loops, you should go back to the problem again and try to solve it with loops, since that does not limit you to a given range for the input integer.

这是有效的,因为你知道这个数字0 到 1000之间。但是一旦你了解了循环,你应该再次回到问题并尝试用循环解决它,因为这不会限制你在给定的范围内输入整数。

回答by Dima

You are almost there. The number has (at most) three digits, right? The right-most digit is number % 10. The left two digits are then twodigits = number / 10, right? So, the next digit on the right, is then twodigits % 10, right? And what's the left most digit? It's twodigits / 10! So:

你快到了。这个数字(最多)有三位数,对吧?最右边的数字是number % 10。那么左边的两位数字是twodigits = number / 10,对吗?那么,右边的下一个数字是twodigits % 10,对吗?最左边的数字是多少?是twodigits / 10!所以:

int rightmost = num % 10;
int twodigits = num / 10;
int middle = twodigits % 10;
int leftmost = twodigits / 10;

int result = leftmost + middle + rightmost;

That's it!

而已!

You could do this with a loop too, as the comments suggest, but, since the problem you quoted limits the number to 1000, and because you seem to not know too much about loops yet, this seems more like what the author of the book you are reading had in mind for this problem.

正如评论所暗示的那样,您也可以使用循环来执行此操作,但是,由于您引用的问题将数量限制为 1000,而且您似乎对循环还不太了解,这似乎更像是本书作者的内容你在阅读时就想到了这个问题。

回答by alizelzele

try recursive methods ( hope u can understand this, cause its harder than for)

尝试递归方法(希望你能理解这一点,因为它比 for 更难)

int digitSum(int number) {
  if (number<10) {
    return number;
  } else {
    int extract = number % 10;
    int remove_extract = number / 10 ;
    return extract + digitSum(remove_extract);
  }
}