判断输入的数字是否为“水仙花数”

思路:1.先输入一个数。

2.分别获取其个位,十位,百位上的数字

3.对其进行判断

代码:

public class Demo1 {
	public static void main(String[] args) {
		System.out.println("请输入需要判断的数:");
		Scanner sc  = new Scanner(System.in);
		int a = sc.nextInt();
		int ge = a%10;
		int shi =a/10%10;
		int bai = a/10/10%10;
		if((ge*ge*ge+shi*shi*shi+bai*bai*bai) == a ) {
			System.out.println(a+"是水仙花数");
		}
		else {
			System.out.println(a+"不是水仙花数");
		}
	}
}

效果:

发布了28 篇原创文章 · 获赞 5 · 访问量 5805

猜你喜欢

转载自blog.csdn.net/weixin_41879980/article/details/95851718
今日推荐