如何在输出中转换长数据类型以使代码工作?

How to convert long data type in output in order for code to work?

在我的代码中,我在参数中包含了一个带有变量的输出方法,问题是它们是不同的数据类型。我知道我可以转换一个来匹配另一个,这样代码就可以工作了,但我不知道如何才能在不影响方程式工作方式的情况下做到这一点。

我试过将阶乘从 long 变量更改为 int 变量,它需要是 long 变量才能使数字达到最大值。

    import java.util.InputMismatchException;
    import java.util.Scanner;

    public class Main 
    {
      public static void main(String[] args)
      {
         System.out.println("\tBasic Factorial Calculator");
         System.out.println();

         int i, number;
         long factorial = 1;

         number = getNumber();
         System.out.println();
         output(number, factorial);
      }

      public static int getNumber()
      {
        int Num = 0;
        boolean done = false;
        Scanner input = new Scanner(System.in);
        while(!done)
        {
          try
          {
            System.out.println("Enter the number you want to find a 
    factorial of: ");
            Num = input.nextInt();
            if (Num <= 0) throw new InputMismatchException();
            done = true;
          }
          catch(InputMismatchException e)
          {
           System.out.println("");
           System.out.println("Error, enter a number greater than 0");
           input.nextLine();
           System.out.println("");
          }
        }
        return Num;
      }
      public static void output(int i, int Num, long factorial)
      {
          System.out.println();
          for(i = 1; i <= Num; i++) 
          {
            factorial = factorial * i;
          }
         System.out.println("The factorial of " + Num + " is " + 
         factorial + ".");
      }
    }

只希望用户能够输入一个数字,然后在包含 try catch 的同时获取它的阶乘:/

n 的阶乘应该只需要一个参数(即 n)。我会 return 阶乘,我会使用 BigInteger 而不是 long。最后,您可以使用 memoization 来提高性能。即利用关系 f(n) = n * f(n - 1)。喜欢,

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("\tBasic Factorial Calculator");
    System.out.println();
    System.out.println("Enter the number you want to find a factorial of: ");
    int num = input.nextInt();
    System.out.println(factorial(num));
}

private static Map<Integer, BigInteger> memo = new HashMap<>();
static {
    memo.put(0, BigInteger.ONE);
    memo.put(1, BigInteger.ONE);
}

public static BigInteger factorial(int num) {
    if (memo.containsKey(num)) {
        return memo.get(num);
    }
    BigInteger v = BigInteger.valueOf(num).multiply(factorial(num - 1));
    memo.put(num, v);
    return v;
}

您的问题出在输出方法上。它的构造函数有三个参数,但你用两个参数调用它。事实上,您一直只需要一个参数。查看我的编辑。

import java.util.InputMismatchException;
import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        System.out.println("\tBasic Factorial Calculator");
        System.out.println();

        int number = getNumber();
        System.out.println();
        output(number);
    }

    public static int getNumber()
    {
        int Num = 0;
        boolean done = false;
        Scanner input = new Scanner(System.in);
        while(!done)
        {
            try
            {
                System.out.println("Enter the number you want to find a factorial of: ");
                Num = input.nextInt();
                if (Num <= 0) throw new InputMismatchException();
                done = true;
            }
            catch(InputMismatchException e)
            {
                System.out.println("");
                System.out.println("Error, enter a number greater than 0");
                input.nextLine();
                System.out.println("");
            }
        }
        return Num;
    }
    public static void output(int Num)
    {
        System.out.println();
        long factorial = 1;
        for(int i = 1; i <= Num; i++)
        {
            factorial = factorial * i;
        }
        System.out.println("The factorial of " + Num + " is " +
                factorial + ".");
    }
}