帮忙用Java编程序

来源:百度知道 编辑:UC知道 时间:2024/05/27 16:41:52
编制程序,打印出100~100000中所有的“水仙花数”。所谓“水仙花数”是指一个3位数,其各位数字的立方和等于该数本身。例如:153是一个“水仙花数”,因为153=13+53+33。

Sxh.java(文件名)
public class Sxh {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int x,y,z,m,i=0;
int [] a=new int[8];
System.out.println("The special numbers are(in the arrange of 1000:\n");
for(m=100;m<1000 ; m++ )
{
x=m/100;
y=(m/10)%10;
z=m%10;
if(x*100+y*10+z==x*x*x+y*y*y+z*z*z)
{
a[i] = m;
i++;
}

}
for(x=0; x<i; x++)
{ System.out.println(a[x]);}
}
}

执行结果:
The special numbers are(in the arrange of 1000:

153
370
371
407