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

本帖最后由 大西洋 于 2015-5-4 22:21 编辑

越来越独立思考解决问题,不再一有问题就百度了:
  1. public class TestIO {
  2.           public static void sumJiShu(){
  3.                 int sum = 0;
  4.                 int count = 0; // 个数统计
  5.                 System.out.println("100以内的奇数为:");
  6.                 //计算1 到  100 奇数 总和
  7.                 for (int i = 0; i <= 100; i++) {
  8.                         if (i % 2 != 0) {
  9.                                 sum += i;
  10.                                 count++; // 每当匹配一个,输出一个
  11.                                 System.out.print(i+" ");
  12.                         }
  13.                 }
  14.                 System.out.print("\r奇数个数为:"+count);
  15.                 System.out.print("\r奇数总和:"+sum);
  16.         }

  17.         public static void main(String[] args) {
  18.                 //readOut();
  19.                 sumJiShu();
  20.         }
  21. }
复制代码

3 个回复

倒序浏览
值得学习ing!
回复 使用道具 举报
  1. public class tellSum {

  2.         public static void main(String[] args) {

  3.                 System.out.println(tellSum(100));
  4.                
  5.         }
  6.         private static int tellSum(int number){
  7.                 int result=-1;
  8.                 if(number<=0){
  9.                         return result;
  10.                 }
  11.                 else{
  12.                         int sum=0;
  13.                         for(int x=0;x<=number;x++){
  14.                                 if(x%2!=0){
  15.                                         sum =sum+x;       
  16.                                 }
  17.                         }
  18.                         return sum;
  19.                 }
  20.         }
  21. }
复制代码

我也做了一下,大家都可以来分享,思路呀,相互借鉴。也希望能够简化代码这块能够有更多的分享。
回复 使用道具 举报
kapp_tolo 发表于 2015-5-11 12:03
我也做了一下,大家都可以来分享,思路呀,相互借鉴。也希望能够简化代码这块能够有更多的分享。 ...

只是做了求和,抱歉这个。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马