求读取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 );
}
}
浙公网安备 33010602011771号