本文主要是介绍java 2.6** Summing the digits in an integer,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
例如 : 999
process: sum=9+9+9=27;
例如 : 932
process: sum=9+3+2=14;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int number;
System.out.println("Enter a number between 0 and 1000: ");
number=input.nextInt();
int sum=0;
while(number!=0)
{
sum=sum+number%10;
number=number/10;
}
System.out.println("The sum of the digits is "+sum);
}
}
这篇关于java 2.6** Summing the digits in an integer的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!