简单的For循环练习7(统计水仙花数有多少个)

需求:统计水仙花数有多少个?
	分析:
		A:首先要知道什么是水仙花数
			所谓的水仙花数是指一个三位数,其各位数字的立方和等于该数本身
			举例:153就是一个水仙花数
			153 = 1*1*1+5*5*5+3*3*3
		定义统计变量,初始化数据是
		B:三位数告诉我们范围,用for循环就可以进行执行
		C:获取每一个三位数的个,十,百的数据
		D:按照要求进行判断
		E:如果满足要求就计数

class ForDemo8{
	public static void main(String[] args){
		int number = 0;
		for(int x = 100;x <= 999;x++){
			int ge = x % 10;
			int shi = x / 10 % 10;
			int bai = x / 10 / 10 % 10;
			if(x == (ge*ge*ge+shi*shi*shi+bai*bai*bai)){
				number = number + 1;
				
			}
			
		}
		System.out.println("水仙花数有:"+number);
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_42322406/article/details/89479447