|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

 

 

posted on   yanqi_vip  阅读(10)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示