Java 返回一个整数的各个数字之和的一种方法

    public static long sumDigits(long n){
        long total=0;
        long number=n;
        while(number!=0){
            total=total+number%10;
            number=(number-number%10)/10;
        }
        return total;
    }
    public static void testSumDigits(){
        System.out.println("Enter a long integer: ");
        Scanner input=new Scanner(System.in);
        long n=input.nextLong();
        long result=sumDigits(n);
        System.out.print("The sum of every number of the integer is:"+ result );
    }
原文地址:https://www.cnblogs.com/xingzhui/p/5700191.html