求读取0-1000之间的整数,将其各位数相加。

//P60   2.6

import java.util.Scanner;
public class Demo_1{
    public static void main(String[] agrs) {
        Scanner input = new Scanner(System.in);
        System.out.("Enter a number 0 and 1000: ");
        int number = input.nextInt();
        int a = number%10;
        int b = number/10;
        int c = number/100;
        
        int sum = a + b + c;
        System.out.println("The sum of the digits is " + sum );
    }
}
posted @ 2017-09-18 12:32  桀与骜  阅读(248)  评论(0)    收藏  举报