[code="java"]//用面向对象的思想实现查找水仙花数的问题 100-999之间的水仙花数
import static java.lang.System.out;
public class F0112
{
/**
* @param args
*/
public static void main(String[] args)
{
for(int i = 100; i<1000; i++)
{
Flower_num fn = new Flower_num(i);
if(fn.getCube()==i);
{
out.println("水仙花数:"+i);
}
}
}
}
/**
- 封装计算水仙花的类
-
@author macleo
/
class Flower_num
{
private int first_bit; //百位上的数字
private int second_bit; //十位上的数字
private int third_bit; //个位上的数字
/*- 水仙花数的构造方法
- @param i / public Flower_num(int i) { this.first_bit = i/100; this.second_bit = i%100/10; this.third_bit = i%100%10; } /*
- 判断是否是水仙花数
- @return boolean */ public boolean Judge() // { return this.getSum()==this.getCube()?true:false; }
/**
- 对各位属性求和
- @return int
*/
public int getSum() //对各位属性求和
{
return this.first_bit*100+this.second_bit*10+this.third_bit;
}
/**
- 对各位属性的立方,求和
- @return int */ public int getCube() // { return this.first_bit*this.first_bit*this.first_bit +this.second_bit*this.second_bit*this.second_bit +this.third_bit*this.third_bit*this.third_bit; } }[/code]