計算一個三位數(shù)各個位數(shù)之和,可以通過以下的Java代碼實現(xiàn)。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("請輸入一個三位數(shù):");
int num = in.nextInt();
int theHundreds = num / 100; //百位數(shù)
int theTens = num % 100 / 10; //十位數(shù)
int theUnits = num % 10; //個位數(shù)
int result = theHundreds + theTens + theUnits; //各個位數(shù)之和
System.out.println("各位數(shù)字之和為:" + result);
}
}
通過以上代碼,我們可以得到輸入的三位數(shù)的百位數(shù)、十位數(shù)和個位數(shù),然后將它們相加即可獲得各個位數(shù)之和。具體可參考代碼中result的計算方式。