簡體   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