任务描述:
输入一个正整数n(取值范围:100<=n<1000),然后输出每位数字之和
任务要求:
代码示例:
package M0317_0331;
import java.util.Scanner;
public class m240327 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入一个三位数整数:");
int num = scanner.nextInt();
if (num < 100 || num > 999) {
System.out.println("输入的数字不符合要求!");
return;
}
//分别获取各位的数字再相加
int a = num / 100;
int b = num % 100 / 10;
int c = num % 10;
System.out.println("各位数字之和为:" + (a + b + c));
}
}