1. 程式人生 > >從控制檯輸入一個五位數,計算各個數位之和

從控制檯輸入一個五位數,計算各個數位之和

import java.util.Scanner;

public class Sum {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("請輸入一個五位數:");
        int wuWeiShu = input.nextInt();
        int geWeiShu = wuWeiShu%10;
        int shiWeiShu = wuWeiShu%100/10;
        
int baiWeiShu = wuWeiShu%1000/100; int qianWeiShu = wuWeiShu%10000/1000; int wanWeiShu = wuWeiShu/10000; int sum = geWeiShu + shiWeiShu + baiWeiShu + qianWeiShu + wanWeiShu; System.out.println(sum); } }

執行結果: