publicclassTest2{publicstaticvoidmain(String[] args){for(int s =100;s<1000;s++){for(int c=1;c<10;c++){//百位上的数字for(int b=0;b<10;b++){//十位上的数字for(int a =0;a<10;a++){//个位上的数字if(s ==100*c +10*b +a & s == c*c*c+b*b*b+a*a*a){System.out.println(s);}}}}}}}
完整代码(方法2):
publicclassTest2{publicstaticvoidmain(String[] args){for(int i =100;i <1000;i++){int c = i/100;//得到百位数int b =(i - c*100)/10;//得到十位数int a = i - c*100- b*10;//得到个位数int res = c*c*c + b*b*b + a*a*a;//各个位数上的立方和if(res == i){//判断是不是水仙花数System.out.println(i);}}}}