Java求出任意数字的各个位数之和

import java.util.Scanner;
/**
 * 用JAVA求任意一个数的各个位数之和
 * @author Administrator
 *
 */
public class test {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.print("请输入一个数字:");
		int num = sc.nextInt();
		int sum=0;
		while(num!=0){
			sum = sum + num%10;
			num = num/10;
		}
		System.out.println("所给数字的各个位数之和为:"+sum);
		
	}

}

你可能感兴趣的:(Java)