我的阶乘方法的实现是对的吗?

时间:2014-10-28 21:39:00

标签: java

我知道如何计算数字的阶乘,但我不熟练将实现细节移到方法中。你能告诉我我的方法是否正确吗?

public class Part3
    {
    public static void main (String args []) 
    {
        Scanner sc = new Scanner(System.in);
        int number;
        int factor=1;
        System.out.println("Enter a number to find the factorial of it: ");
        number= sc.nextInt();
    }
    public static int factorial (int number, int factor, int x)
    {
        if (number < 1 || number > 10)
            System.out.println("Invalid!! the number has to be between 1 and 10");
        else 
        {
            for(x=1; x<=number; x++ )
            {
                factor = factor*x;
            }
        }
    }
    System.out.println("The factorial of  "+x+" is = " +factor);
}

4 个答案:

答案 0 :(得分:1)

一些事情:

  • 当你创建一个函数时,你必须总是返回该类型的值,你声明你的方法返回int所以你必须总是返回一个int(或者你会得到编译器错误)
  • 此外,您可以在方法中声明变量,不需要从主体中传递所有变量。
  • 不要忘记在您的主电话中调用您的功能,否则它不会被使用。

这将是代码

public class Part3 {
    public static void main (String args [])  {
        Scanner sc = new Scanner(System.in);
        int number;
        int factor=1;
        System.out.println("Enter a number to find the factorial of it: ");
        number= sc.nextInt();
        factor=factorial(number);
        System.out.println("The factorial of  "+number+" is = " +factor);
    }
    public static int factorial (int number) {
        int result = 1;
        if (number < 1 || number > 10)
            System.out.println("Invalid!! the number has to be between 1 and 10");
        else  {
            for(int x=1; x<=number; x++ ) {
                result= result*x;
            }
        }
        return result;
    }
}

答案 1 :(得分:0)

您没有在任何地方呼叫public static int factorial (int number, int factor, int x)。这里使用这个逻辑来提出用于查找n阶乘

的函数/递归代码

任何方法声明都包含以下部分:

&#34;访问修饰符&#34; &#34;返回数据类型&#34; &#34;方法名称&#34; (参数列表)

有关详细信息,请参阅this

public int factorial(int n)
{
    return n * Factorial(n-1);
}

以上代码永远不会停止。所以你需要一个边界条件。试着想出一个。快乐的编码!!

答案 2 :(得分:0)

int x参数将始终被for(x = 1 ... ...所以这可能是正确的。

BTW,要验证您的方法,请创建单元测试:

import static org.junit.Assert.assertEquals;
import org.junit.Test;

class Part3Test
{
    public static void testFactorial()
    {
        assertEquals(Part3.factorial(1, 1, 1), 1);
        assertEquals(Part3.factorial(2, 1, 1), 2);
    }
}

答案 3 :(得分:-1)

我担心你的方法不是很好: - (

更严重的是,您只将它用作静态函数,声明它返回一个int并且您没有返回语句。您传递了方法中未使用的2(输入)参数factor和x。

如果你想让它成为一个没有副作用的简单实用方法,也不想使用对象的state属性,你至少可以这样做:

public static int factorial (int number) // only one parameter
{
    int factor = 1; //the result - must be initialized to 1 since you use it later
    if (number < 1 || number > 10) {
        System.out.println("Invalid!! the number has to be between 1 and 10");
        factor = -1;
    }
    else 
    {
        for(int x=1; x<=number; x++ ) // x can be declared in the loop
        {
            factor = factor*x;
        }
    }
    return factor;
}

并以这种方式使用:

    System.out.println("Enter a number to find the factorial of it: ");
    number= sc.nextInt();
    int factor = factorial(number);
    System.out.println("The factorial of  "+number+" is = " +factor); // print in the main

如果您想将它用作修改对象的方法,您可以尝试:

public class Part3
    {
    private int factor;

    public static void main (String args []) 
    {
        Scanner sc = new Scanner(System.in);
        int number;
        Part3 part3 = new Part3(); // create an object
        System.out.println("Enter a number to find the factorial of it: ");
        number= sc.nextInt();
        part3.factorial(number)
        System.out.println("The factorial of  "+number+" is = " +part3.factor);
    }
    public void factorial (int number)
    {
        factor = 1; // already exists in the object
        if (number < 1 || number > 10) {
            System.out.println("Invalid!! the number has to be between 1 and 10");
            factor = -1;
        }
        else 
        {
            for(int x=1; x<=number; x++ ) // x can be declared in the loop
            {
                factor = factor*x;
            }
        }
    }
}