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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

我写了一串代码,是方法getsum调用方法jiecheng的时候,为什jiecheng作为参数同时也是一种方法为什么下面箭头的位置不用加(),开始我加了()编译通不过,我试试去了()就没事了 结果是1409286144
        //求1!+2!+3!+4!.......+30!之和并显示
public class firstday {
        public static void main(String[] args) {
                jiecheng(30);
                System.out.println(getsum(jiecheng(30)));
        }
       
        public  static int jiecheng(int a){
                if(a==1){
                        return 1;
                }else{
                        return a*jiecheng(a-1);
                }
        }

        public  static int getsum(int jiehcneg){//就是这里  jiecheng后面为什么不用带()
                for(int b=1;b<31;b++){
                        int sum=0;
                        sum=sum+jiecheng(b);
                }
                return jiehcneg;
        }

}

15 个回复

倒序浏览
是 jiehcneg     //int型的
不是jiecheng()~~!//函数
回复 使用道具 举报 1 0
挺对不住,我再发一次代码
        //求1!+2!+3!+4!.......+30!之和并显示
public class firstday {
        public static void main(String[] args) {
                jiecheng(30);
                System.out.println(getsum(jiecheng(30)));
        }
       
        public  static int jiecheng(int a){
                if(a==1){
                        return 1;
                }else{
                        return a*jiecheng(a-1);
                }
        }

        public  static int getsum(int jiecheng){
                for(int b=1;b<31;b++){
                        int sum=0;
                        sum=sum+jiecheng(b);
                }
                return jiecheng;
        }

}

回复 使用道具 举报
你的意思 在一个方法中调用另一个方法 另一个方法是被看作是参数,故尽管是方法但是当作参数的时候是不能加()
回复 使用道具 举报
感觉有点凌乱
回复 使用道具 举报
getsum(int jiecheng)  括号里的只是一个传入运算的参数而已,你把他改成a,b或者c都没关系的,与你的jiecheng()方法没有半毛钱关系,不要被误导了~~
回复 使用道具 举报
  1. package com.itheima.test;

  2. public class firstday {
  3.         public static void main(String[] args) {
  4.                 System.out.println(getsum(jiecheng(3))); // 结果6
  5.         }

  6.         public static int jiecheng(int a) {
  7.                 if (a == 1) {
  8.                         return 1;
  9.                 } else {
  10.                         return a * jiecheng(a - 1);
  11.                 }
  12.         }

  13.         public static int getsum(int c) {
  14.                 for (int b = 1; b < 31; b++) {
  15.                         int sum = 0;
  16.                         sum = sum + jiecheng(b);
  17.                 }
  18.                 return c;
  19.         }

  20. }
复制代码
回复 使用道具 举报
明白你意思了,你下边举得例子就是告诉我里面不是就是一个参数嘛
回复 使用道具 举报
public class firstday {
        public static void main(String[] args) {
               
                System.out.println(getsum(30));
        }
        
        public  static int jiecheng(int a){
                if(a==1){
                        return 1;
                }else{
                        return a*jiecheng(a-1);
                }
        }

        public  static int getsum(int x){
                for(int b=1;b<x+1;b++){
                        int sum=0;
                        sum=sum+jiecheng(b);
                }
                return sum;
        }

}
这样改才行吧,你那样的话只是返回30!,没有和。
回复 使用道具 举报
如果只改 return sum,不过这样的话,不管你传的参数是30还是什么,程序就只能求1!+2!+3!+4!.......+30!之和了,因为你最后传的int jiecheng 跟函数根本没有联系。
回复 使用道具 举报
楼主要明白一个问题,在你用jicheng方法进行计算的时候会得出来一个结果,这个结果就是你返回的当前数字的阶乘,是一个具体的数,所以当你求和的时候只需要给出需要算到哪一位阶乘的数字就可以了,而不是传入一个方法,因为该方法已经返回一个具体的数字,作为sum求和的一部分。
同时9楼的代码也是有问题的,sum定义在局部中是没法返回的。具体代码如下:
  1. class firstday {
  2.     public static void main(String[] args) {
  3.             jiecheng(30);
  4.             System.out.println(getsum(jiecheng(30)));
  5.     }
  6.    
  7.     public  static int jiecheng(int a)
  8.     {
  9.             if(a==1)
  10.             {
  11.                   return 1;
  12.             }
  13.             else
  14.             {
  15.                   return a*jiecheng(a-1);
  16.             }
  17.     }
  18.     //1409286144
  19.     public  static double getsum(int jiehcneg)
  20.     {
  21.             //就是这里  jiecheng后面为什么不用带()
  22.                     double sum=0;
  23.             for(int b=1;b<31;b++)
  24.             {
  25.                  sum=sum+jiecheng(b);
  26.             }
  27.             return sum;
  28.     }

  29. }
复制代码
回复 使用道具 举报
没看懂...
回复 使用道具 举报
huangchunwei 来自手机 中级黑马 2015-3-3 08:00:44
13#
{:3_47:}{:3_47:}{:3_47:}{:3_47:}
回复 使用道具 举报
誓进黑马,板凳飘过
回复 使用道具 举报
你确定int类型能装得下?
回复 使用道具 举报
你的jiecheng是个方法  不是个参数呢
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马