繁体   English   中英

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

[英]Can't print the result on the eclipse console

你好,我正在尝试创建一个程序来计算一个数字的阶乘。 控制台不显示任何错误,但也不显示任何结果。 我究竟做错了什么? 谢谢

    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;

    }


}

你永远不会调用 Faculty(...) 方法:

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

您还将摆脱:

n=5;

如果您总是硬编码一个值,那将违背将参数传递给方法的目的。

另外,您为什么称该方法为“教师”? 也许方法名称更具描述性,例如calculateFactorial ,因为这就是您要计算的内容。 使您的方法名称具有描述性,以便我们了解该方法的作用。

暂无
暂无

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

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