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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© xtf 中级黑马   /  2015-6-7 11:27  /  610 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

public class test3 {
        public static void main(String[] args) {
                int a=10,b=12,sum=0;
                for (int i = 0; i < 100; i++) {
                        sum=sum+b;       //sum为12+22+32+...+1002
                        b=b+a;          //b的值为12,22,32。。。1002
                }
                System.out.println(sum);
               
               
        }

}

5 个回复

倒序浏览
将楼上的改成while循环:

        public static void main(String[] args) {
                int sum = 0;
                int x = 12;
                while(true){
                        if (x <= 1002) {
                                sum += x;
                                x += 10;
                        } else {
                                break;
                        }
                }
                System.out.println(sum);
        }
}
回复 使用道具 举报
二楼方法简便
回复 使用道具 举报
可以优化成递归做
回复 使用道具 举报
都是大神哈
回复 使用道具 举报
赞一个,大神啊 啊啊啊啊
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马