基础Java练习15:打印水仙花数

编写程序

/**
 * 功能:打印水仙花数
 * 作者:孤梦
 * 日期:2022年04月07日
 */
public class Example13 {
    
    
    public static void main(String[] args) {
    
    
        int p1,p2,p3,count;
        count=0;
        for (int n = 100;n <= 999;n++) {
    
    
            p1 = n % 10;
            p2 = n / 100;
            p3 = n % 100 / 10 ;
            if (n == p3*p3*p3 + p2*p2*p2 + p1*p1*p1) {
    
    
                count++;
                System.out.println(n +"="+p1 + "^3"+ p2 + "^3"+p1+"^3");
            }
        }
        System.out.println("一共有"+count+"水仙花数");
    }

}

运行程序,查看结果

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_62491692/article/details/124474137