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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 大头兵123456789 中级黑马   /  2015-9-30 20:15  /  996 人查看  /  15 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

打印水仙花数,统计水仙花个数求详细.

15 个回复

倒序浏览
class Test1_While {
        public static void main(String[] args) {
               
                int count = 0;                                       
   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) {
                                count ++;
                        }

                        i++;
                }

                System.out.println("count =" + count);

                                }
        }
}
回复 使用道具 举报
int count=0;          for (int i=100;i<=999;i++ ) {                  int g=i % 10;                  int s=i / 10 % 10;                  int b=i / 100;                  if (i == g*g*g+s*s*s+b*b*b) {                          System.out.println(i);                           count++;                     }                                     }                  System.out.println("count = " +count);
回复 使用道具 举报
         int count=0;      //定义count变量来记录水仙花数的个数
         for (int i=100;i<=999;i++ ) {
                 int g=i % 10;               //个位
                 int s=i / 10 % 10;         //十位
                 int b=i / 100;               //百位
                 if (i == g*g*g+s*s*s+b*b*b) {
                         System.out.println(i); //将适合的水仙花数输出在控制台上
                         count++;
                    }
                  
                 }
                 System.out.println("count = " +count);//输出水仙花数的个数
回复 使用道具 举报
这个题目上课都会讲吧?
回复 使用道具 举报
来学习学习!!!!!!!
回复 使用道具 举报
课程里面有。。而且这个问题百度能出一大堆的。。。
回复 使用道具 举报
这个上课的时候有讲的吧?
class Test1_While {
        public static void main(String[] args) {
               
                int count = 0;                        //定义变量记录个数               
   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) {
                                count ++;
                        }

                        i++;
                }

                System.out.println("count =" + count);

                   }
        }
}
回复 使用道具 举报
class Flower
{
       public static void main(String[] args)
       {
               //因为是三位数 范围在100-999
               for(int i=100;i<=999;i++)
               {
                       //分别求出每次i的个位、十位、百位上的数是多少
                       int ge = i%10;
                       int shi = i/10%10;
                       int bai = i/100%10;
                       //判断是否是水仙花 如果是输出这个数
                       if(ge*ge*ge+shi*shi*shi+bai*bai*bai==i)
                       {
                                System.out.println("水仙花数: "+i);
                       }
               }
       }
}
回复 使用道具 举报
谢谢给位同学
回复 使用道具 举报
把别人发的好好看看,这个是个面试题
回复 使用道具 举报
学习到了~!
回复 使用道具 举报
前来学习~~~
回复 使用道具 举报
长知识了
回复 使用道具 举报
import java.util.Scanner;
class ShuiXianHuaShu
{
        public static void main(String[] args)
        {
                Scanner sc=new Scanner(System.in);
                System.out.println("请输入一个100-1000间的数字");
                int a=sc.nextInt();
                if (a>=100 && a<1000)
                {
                int x=a%10;
                int y=a/10%10;
                int z=a/100;
                if (a==x*x*x+y*y*y+z*z*z)
                {
                        System.out.println(a+"是水仙花数");
                }
                else
                        System.out.println(a+"不是水仙花数");
                }
                else
                {
                        System.out.println("请输入有效的数据");
                }
               
        }
}
回复 使用道具 举报
//找出100-1000之间的所有水仙花数。
class ShuiXianHuaShu1
{
        public static void main(String[] args)
        {
                int a=0;
                for (int i=100;i<1000 ;i++ )
                {
                int x=i%10;
                int y=i/10%10;
                int z=i/100;
                        if (i==x*x*x+y*y*y+z*z*z)
                        {
                                a++;
                                System.out.println(i);
                        }
                }
                System.out.println("总共有"+a+"个水仙花数");
        }
}
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马