|NO.Z.00065|——————————|BigDataEnd|——|Java&循环结构.V04|——|Java.v04|for循环.v04|实现水仙花数打印|
一、for循环实现水仙花数打印
### --- 案例题目
~~~ ——> 使用for循环打印三位数中所有水仙花数。
~~~ ——> 所谓“水仙花数”即一个整数满足其值等于各个数位的立方和。
~~~ ——> 如:153是一个水仙花数,因为153=1^3+5^3+3^3。
二、编程代码
### --- 编程代码
/*
编程使用for循环打印三位数中的所有水仙花数
*/
public class ForWaterTest {
public static void main(String[] args) {
// 1.使用for循环打印所有的三位数
for(int i = 100; i <= 999; i++) {
// 3.拆分三位数中的各个数位上的数字
// 123 / 100 = 1; 123 % 100 => 23 / 10 = 2; 123 % 10 = 3;
int ia = i / 100; // 拆分百位数
int ib = i % 100 / 10; // 拆分十位数
int ic = i % 10; // 拆分个位数
// 2.针对每个三位数都要判断该数是否为水仙花数,若是则打印,否则不打印
// 判断该数是否等于各个数位的立方和
if((ia*ia*ia + ib*ib*ib + ic*ic*ic) == i) {
System.out.println("i = " + i);
}
}
}
}
三、打印输出
### --- 编译
C:\Users\Administrator\Desktop\project>javac ForWaterTest.java
### --- 打印输出
C:\Users\Administrator\Desktop\project>java ForWaterTest
i = 153
i = 370
i = 371
i = 407
Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
——W.S.Landor
浙公网安备 33010602011771号