java基础50题(3)

时间:2023-02-17 12:10:05

/*
 * 03:打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。
 */

这道题是个水题,不多作解释。直接挂代码:

<span style="font-size:18px;">public class Question3 {
public static void main(String[] args){
int i;
for(i=100;i<=999;i++){
if(i==(int)(Math.pow(i/100, 3)+Math.pow((i/10)%10, 3)+Math.pow(i%10,3)))
System.out.println(i);
}
}
}</span>
唯一值得注意的地方是,Math.pow()返回的是double型的数值,要将结果强制转换为int。