java多循环水仙花数程序哪里错了?

来源:百度知道 编辑:UC知道 时间:2024/05/31 07:01:18
public class J1{
public static void main(String[] args){
int a, b, c;
double d;
for(d = 100; d <= 999; d++){
for(a = 1; a <= 9; a++){
for(b = 0; b <= 9;b++){
for(c = 0; c <= 9; c++){
d = a * 100 + b * 10 + c;
d = Math.pow(a,3) + Math.pow(b,3) + Math.pow(c,3);
System.out.println((int)d);
}
}
}
}
}

}

实际上你要得出的值已经出来了,但是你把很多不必要的值输出来了,现在加上一个判定条件就好了,你自己看下
public class J1 {

public static void main(String[] args){
int a, b, c;
double d;
for(d = 100; d <= 999; d++){
for(a = 1; a <= 9; a++){
for(b = 0; b <= 9;b++){
for(c = 0; c <= 9; c++){
d = Math.pow(a,3) + Math.pow(b,3) + Math.pow(c,3);
if(d==100*a+10*b+c){
System.out.println((int)d);
}
}
}
}
}
}

}