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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© shiax11 中级黑马   /  2015-12-26 00:38  /  1616 人查看  /  16 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

class ForDemo1 {
        public static void main(String[] args) {
                //定义统计变量,初始化值是0
                int count = 0;
               
                //三位数告诉了我们范围,用for循环就可以搞定
                for(int x=100; x<1000; x++) {
                        //获取每一个三位数的个,十,百的数据
                        int ge = x%10;
                        int shi = x/10%10;
                        int bai = x/10/10%10;
                       
                        //按照要求进行判断
                        if(x == (ge*ge*ge+shi*shi*shi+bai*bai*bai)) {
                                //如果满足要求就计数。
                                count++;
                        }
                }
               
                System.out.println("水仙花数共有"+count+"个");
        }
}

16 个回复

倒序浏览
恩 不错
很好很好
回复 使用道具 举报
其实那个x==后面不用小括号 {:2_40:}
回复 使用道具 举报
这个不好玩!看这个
import java.util.Scanner;
class  Test_0909{
        public static void main(String[] args) {
                for (; ; )
                {
                Scanner sc = new Scanner(System.in);
                System.out.print("请输入一个三位数字:");
                int x = sc.nextInt();
                judgement(x);
                }

        }
        public static void judgement(int x){
               
                if(x >= 1000 || x <= 99){
                        System.out.println("你是傻叉吗?说了三位数!!");
                } else{
                        int a = x % 10;
                        int b = x / 10 % 10;
                        int c = x / 100;
                        if(a*a*a + b*b*b + c*c*c == x){
                                System.out.println(x + "是水仙数!");
                        }else{
                                System.out.println(x + "不是水仙数!");       
                        }
                       
                }
        }
}
回复 使用道具 举报
............
回复 使用道具 举报
一个足以
回复 使用道具 举报
12.21期你是谁
回复 使用道具 举报
爱笑的人运气不会太差
回复 使用道具 举报
anuo 中级黑马 2015-12-27 01:29:15
9#
qinrongchang 发表于 2015-12-26 22:09
其实那个x==后面不用小括号

楼主是觉得括起来更安全
回复 使用道具 举报
老师前天也讲过这道面试题,开始还{:2_41:}感觉有点晕
回复 使用道具 举报
anuo 发表于 2015-12-27 01:29
楼主是觉得括起来更安全

恩恩 谢谢提醒
回复 使用道具 举报

我还在学  准备明年4月分去
回复 使用道具 举报
qinrongchang 发表于 2015-12-26 22:09
其实那个x==后面不用小括号

怕搞不清 。。。。。
回复 使用道具 举报
赞一个
回复 使用道具 举报
chengz 中级黑马 2015-12-27 23:11:36
15#
666666666666
回复 使用道具 举报
shiax11 发表于 2015-12-27 20:19
我还在学  准备明年4月分去

直接就业班?还不如年后来上基础班呢。
回复 使用道具 举报
最好的百位直接除100不是更方便吗?多写一个/10不是,增大一点cpu的内存消耗吗?
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马