import java.util.Scanner; public class add { public static int sumDigits(long n){ int sum=0; while(n>0){ int m=(int)(n%10); sum=sum+m; n=n/10; } return sum; } public static void main(String[] args) { try{ System.out.println("請輸入數字:"); Scanner sc=new Scanner(System.in); long l=sc.nextLong(); System.out.println("輸入數字的各位數之和為"+sumDigits(l)); }catch(Exception e){ System.out.println("您輸入的不是數字!"); } } }