Java——数字提取:任给一个整数,输出各位数据,并计算各个位的和值。
package sl.dong;
import java.util.Scanner;
public class NumberSum{
public static void main(String[] args){
int num;
int sum = 0;
System.out.println("输入一个数:");
Scanner cin=new Scanner(System.in);
num=cin.nextInt();
while(num != 0){
int digit = num % 10;
sum = sum + digit;
num = num / 10;
System.out.println("digit: "+digit);
}
System.out.println("sum: "+sum);
}
}