简体   繁体   English

无法在 eclipse 控制台上打印结果

[英]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:你永远不会调用 Faculty(...) 方法:

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.也许方法名称更具描述性,例如calculateFactorial ,因为这就是您要计算的内容。 Make you method names descriptive so we have an ideas what the method does.使您的方法名称具有描述性,以便我们了解该方法的作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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