Java 求一个百位数各个位置上的数字的和

public class Demo{
		public static void main(String[] args) {
			int  a = 123;  //初始化一个三位数  以123为例
			int  b = a/100;  //b=1  拿到百位上的数字
			int  c = a%100;//c=23  拿到十位和个位的数字
			int  d = c/10;//d=2	拿到十位上的数字
			int  e = c%10;//e=3	拿到个位上的数字
			int  f = b+d+e; // 求和
			System.out.println("百位上的数是:"+b+"十位上的数是:"+d+"各位上的数是:"+e);
			System.out.println("每个位上的数相加是:"+f);
	}
}

猜你喜欢

转载自blog.csdn.net/Mr_tie/article/details/90105601