A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© liuzegang 中级黑马   /  2016-2-9 23:32  /  1946 人查看  /  11 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

public class Prog3{
        public static void main(String[] args){
                for(int i=100;i<1000;i++){
                        if(isLotus(i))
                           System.out.print(i+" ");
                }
                System.out.println();
        }
        //判断水仙花数
        private static boolean isLotus(int lotus){
                int m = 0;
                int n = lotus;
                int sum = 0;
                m = n/100;
                n  -= m*100;
                sum = m*m*m;
                m = n/10;
                n -= m*10;
                sum += m*m*m + n*n*n;
                if(sum==lotus)
                        return true;
                else
                        return false;
                }
}

11 个回复

倒序浏览
给你点个赞
回复 使用道具 举报
你这个跟我们学的不一样 啊啊啊
回复 使用道具 举报
我咋记得我学的时候  没这么复杂呢
回复 使用道具 举报
class ShuiXianHua {
        public static void main(String[] args) {
                int x,y,z,a,c=0;
                for (a = 100; a<1000 ;a++ ) {

                        x = a % 10;                                //个位
                        y = a / 10 % 10;                //十位
                        z = a / 100;                                //百位
                        if (a==(x*x*x) + (y*y*y) + (z*z*z)){
                                System.out.println(a);
                                c++;
                        }
                }
                System.out.println("水仙花数个数为:" + c);
        }
}
回复 使用道具 举报
创建了一个函数 判断是否为水仙数 返回值为boolean型
回复 使用道具 举报
0308lidong 来自手机 中级黑马 2016-3-24 12:17:30
7#
好好学习,天天向上
回复 使用道具 举报
Andyhuai 发表于 2016-3-11 20:46
class ShuiXianHua {
        public static void main(String[] args) {
                int x,y,z,a,c=0;

这个正解 。楼主你这个写的太乱了,有点拖拉,直接取模后判断就行
回复 使用道具 举报
跟我们的方法不一样,,,不过很不错,,,值得一赞····
回复 使用道具 举报
不错,正解
回复 使用道具 举报
方法都一样,可是格式确实看开发语言的
回复 使用道具 举报
好吧 给你赞一个
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马