需求:三种循环方式求水仙花数
public class WaterFlower { public static void main(String[] args) {
for (int i = 100; i <= 999; i++) {
int ge = i % 10;
int shi = i / 10 % 10;
int bai = i / 100;
if (i == ge * ge * ge + shi * shi * shi + bai * bai * bai) {
System.out.println(i + "是水仙花数!");
}
}
System.out.println("========================");
int i = 100;
while (i <= 999) {
int ge = i % 10;
int shi = i / 10 % 10;
int bai = i / 100;
if (i == ge * ge * ge + shi * shi * shi + bai * bai * bai) {
System.out.println(i + "是水仙花数!"); }
i++;
}System.out.println("=====================");
int b=100;
do { int ge = b % 10;
int shi = b / 10 % 10;
int bai = b / 100;
if (b == ge * ge * ge + shi * shi * shi + bai * bai * bai)
System.out.println(b + "是水仙花数!");b++;
}while (b<=999);
}
}
|
|