1、输入一个四位数的整数,要求编程将这个四位数中的个位,十位,百位,千位分别输出,求和。

这个是一个运算逻辑问题,看到的小伙伴应该是还没有弄懂“/”和“%”两个运算符的计算

那么首先讲解一下“/”运算符

想要理解它很简单:

package test;


public class sdf {
	public static void main(String[] args) {
		int a = 1234;
		int b = a/10;
		int c = a/100;
		int d = a/1000;
		int e = a/10000;
		System.out.println(b);
		System.out.println(c);
		System.out.println(d);
		System.out.println(e);
	}
}

 可看到结果得出:取的是整数部分,也就是砍尾(小数点后面)

那我们看看“%”运算符

package test;


public class sdf {
	public static void main(String[] args) {
		int a = 1234;
		int b = a%10;
		int c = a%100;
		int d = a%1000;
		int e = a%10000;
		System.out.println(b);
		System.out.println(c);
		System.out.println(d);
		System.out.println(e);
	}
}

 可看到结果得出:取的小数部分,也就是取尾(小数点后面)

那我们就可以看看最后的代码: 

package test;


public class sdf {
	public static void main(String[] args) {
		int a = 1234;
		int b = a%10;//取个位
		int c = a%100/10;//取十位
		int d = a/100%10;//取百位
		int e = a/1000;//取千位
		int count = b+c+d+e;
		System.out.println("个位:"+b);
		System.out.println("十位:"+c);
		System.out.println("百位:"+d);
		System.out.println("千位:"+e);
		System.out.println("和"+count);
	}
}

 为了更好的理解,我举其中一个运算来看看:取十位

取十位:

        int c = a%100/10;

        1234%100时  =  12.34

        获取到小数后面几位34

        再用34/10  =  3.4

        获取到整数部分3。

按照这样的思维我想后面的百位和千位会很好的了解了吧

好了如果有更好的思维欢迎大家评论留言,下期见

        

  • 5
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值