打印水仙花数
打印出三位数的水仙花数
求 各位大哥大姐指点一下错误、、、谢谢
public class ShuiXian {
public static void main(String[] args) {
int a,b,c;
int sum,title;
for(a=1;a<=9;a++){
for(b=0;b<=9;b++){
for(c=0;c<=9;c++){
sum = 100*a+10*b+c;
title=a*a*a+b*b*b+c*c*c;
if(sum == title){}
System.out.println(sum);
}
}
}
}
}
------解决方案--------------------我把我的发上来、你看一下吧、
import java.util.Calendar;
import java.util.Date;
public class Shuixian {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
for (int i = 101; i < 99999; i++) {
shuixian(i);
}
}
public static void shuixian(int i) {
int i0 = i;
String s = i + "";
int le = s.length();
int le1 = le;
int sum = 0;
boolean flag = true;
while (flag) {
int ci = (int) Math.pow(10, le);
int i1 = i / ci;
int i2 = i % ci;
int su = (int) Math.pow(i1, le1);
sum = sum + su;
i = i2;
s = "" + i;
le = s.length() - 1;
if (le == 0) {
su = (int) Math.pow(i2, le1);
sum = sum + su;
if (sum == i0) {
System.out.println(sum);
}
flag = false;
}
}
}
}
------解决方案--------------------if(sum == title)后面多了{} 去掉就可以了