java语言程序设计 课后习题
自己纯手工,欢迎讨论
package chapter6;
import java.util.Scanner;
public class Test2 {
public static void main(String[] args) {
@SuppressWarnings("resource")
Scanner in = new Scanner(System.in);
System.out.println("Enter a number:");
long n = in.nextLong();
System.out.println("The sum of digits of the number: " + Test2.sumDigits(n));
}
public static int sumDigits(long n) {
int sum = 0;
while (true) {
if (n / 10 == 0) {
sum += n;
break;
}
sum += (n % 10);
n /= 10;
}
return sum;
}
}