要求用户输入一个四位数,将这个四位数的个,十,百,千位的数字单独输出来,并将他们的和求出来! 例:用户输入2045,输出结果为:个位:5,十位:4,百位:0,千位:2,和为:11

package com.bwj;

import java.util.Scanner;

public class Num {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner Num = new Scanner(System.in);
		int a = Num.nextInt();
		int b = a % 10;
		int c = a / 10 % 10;
		int d = a / 100 % 10;
		int f = a / 1000 % 10;
		int g = b + c + d + f;
		System.out.println("输出结果为:个位:" + b + ",十位:" + c + ",百位:" + d + ",千位:" + f + ",和为:" + g);
	}

}

输出结果为:



你可能感兴趣的:(Java)