Java基础练习题-数字各个位数相加
Integer types and operations: The sum of digits
题目要求:给定一个三位数(100-999)。求其各个位数的和。(要求效率4星,满星四星)
方法一:单纯使用基本算术运算符进行计算。
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int num = scanner.nextInt();
System.out.println(num / 100 + num % 100 / 10 + num % 10);
}