思路:
1.设置取值范围,水仙花数是三位数,故i取值100~999
2.求取百位数
3.求取十位数
4.求取个位数
5.判断i是否为水仙花数
6.打印水仙花数
代码:
public static void main(String[] args) {
for(int i =100;i<1000;i++){ //水仙花数是三位数,故i取值100~999
int a = i/100; //求取百位数
int b = i%100/10; //求取十位数
int c = i%10; //求取个位数
int temp = a*a*a+b*b*b+c*c*c;
/*如果temp==i,则该数是水仙花数.*/
if(temp==i){
System.out.println(i); //打印水仙花数
}
}
}