简体   繁体   中英

Can't print the result on the eclipse console

Hello, I'm trying to create a program to calculate the factorial of a number. The console doesn't show any errors but also doesn't show any results. What am I doing wrong? Thank you

    public static void main(String[] args) {
    }

    public static int faculty(int n) {
       n=5;
        int x = 1;
        if(n > 0){
            for(int i = 1; i <= n; i++){
                x = x*i;
            }

        }
        System.out.println(x);
        return x;

    }


}

You never invoke the faculty(...) method:

public static void main(String[] args) 
{
    faculty( 5 );
}

You would also get rid of:

n=5;

That defeats the purpose of passing a parameter to the method if you always hard code a value.

Also, why would you call the method "faculty"? Maybe something more descriptive like calculateFactorial for the method name, since that is what is what you are trying to calculate. Make you method names descriptive so we have an ideas what the method does.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM