1 import java.util.Scanner; 2 /** 3 * @author 冰樱梦 4 * 时间:2018年下半年 5 * 题目:求一个整数个位数之和 6 * 7 */ 8 public class Exercise06_02 { 9 public static void main(String[] args){ 10 Scanner input = new Scanner(System.in); 11 System.out.println("输入一个整数: "); 12 long n=input.nextLong(); 13 System.out.println("各位数字和为:" + sumDigits(n)); 14 } 15 public static long sumDigits(long n){ 16 long sum=0,all=0; 17 int a=1; 18 long l = (n+" ").length()-1; 19 for(int i=1;i<=l;i++){ 20 sum=(long)(n/(Math.pow(10, l-a))); 21 n=(long) (n%(Math.pow(10, l-a))); 22 a++; 23 all+=sum; 24 } 25 return all; 26 } 27 }