【问题标题】:Factorial Java Program阶乘 Java 程序
【发布时间】:2014-01-31 14:44:22
【问题描述】:

我想用 for 循环在 java 中做一个阶乘程序。例如,我想获取用户输入,比如说10,然后乘以10*9*8*7*6*5*4*3*2*1。我需要帮助构建for 循环。到目前为止,我不知道该去哪里,所以下面的代码就是我所拥有的。

import java.util.Scanner;
import java.lang.Math;
public class factorial {

    public static void main(String[] args) {
        int num;
        Scanner input = new Scanner(System.in);
        System.out.println("Enter a number: ");
        num = input.nextInt();
    }
}

【问题讨论】:

标签: java for-loop factorial


【解决方案1】:

递归函数编码:

import java.util.Scanner;

public class Main {

     public static void main(String[] args)
     {
         System.out.print("Enter a number: ");
         Scanner input = new Scanner(System.in);
         int num = input.nextInt();
         long factorialResult = factorialRecursive(num);
         System.out.println(factorialResult);
     }


     public static long factorialRecursive(int n) {
         if (n == 0 || n == 1 )
             return 1;

         return n * factorialRecursive(n - 1);
     }
}

来源:learn.uncox.com

如果数字很大,就会发生“stackoverflow”。

【讨论】:

  • 0 的阶乘是 1。您的代码没有考虑到这一点。基本情况应为n == 0
【解决方案2】:

试试

public static void main(String[] args) {
    int num;
    int fact=1;
    Scanner input = new Scanner(System.in);
    System.out.println("Enter a number: ");
    num = input.nextInt();
    for (int i=2;i<=num; i++){
        fact=fact*i;
    }

    System.out.println("Factorial: "+fact);
}

正如@Marko Topolnik 在 cmets 中提到的,此代码适用于最多 12 个输入。对于较大的输入,由于溢出,将输出无穷大。

对于大于 12 的数字,您应该使用更高的数据类型,例如 BigInteger

你可以试试:

public static void main(String[] args) {
    BigInteger num;
    BigInteger fact = BigInteger.valueOf(1);
    Scanner input = new Scanner(System.in);
    System.out.println("Enter a number: ");
    num = input.nextBigInteger();
    for (int i = 2; i <= num; i++){
        fact = fact.multiply(BigInteger.valueOf(i));
    }
    System.out.println(fact);
}

【讨论】:

  • 这样的家庭作业的重点不是让学生记住语法,而是通过将问题分解成更小的部分并自行处理每个部分的过程来完成。通过发布完整的代码解决方案,您已经剥夺了该过程的 OP,从长远来看这将伤害他/她。下次 OP 必须解决问题时会发生什么?测试期间会发生什么?推荐阅读:meta.stackexchange.com/questions/10811/…
  • 请注意,您的代码仅适用于最多 12 个输入。绑定检查可能是个好主意...
  • @KevinWorkman 阶乘计算代码不是完全机密的信息。 laaposto 所做的只是剥夺了 OP 在谷歌上搜索它的尝试。你可以以道德为由攻击他,但不能泄露一个大秘密。
  • @Eqorip 虽然我对你的情况表示同情,但请记住,很多人完全靠自己学习编程,没有课堂、同学、辅导课程、学校图书馆或任何在教室里带来的其他好处。随意使用全代码解决方案,但我会警告您,90% 的编程都是在解决问题,将其分解成更小的部分,并独立处理每个部分。记忆语法是另外 10%。祝你好运。
  • 不错的 OP。像魅力一样工作。
【解决方案3】:

何必计算呢?

public int factorial ( int n ) {
switch(n){
case 0: return 1;
case 1: return 1;
case 2: return 2;
case 3: return 6;
case 4: return 24;
case 5: return 120;
case 6: return 720;
case 7: return 5040;
case 8: return 40320;
case 9: return 362880;
case 10: return 3628800;
case 11: return 39916800;
case 12: return 479001600;
default : throw new IllegalArgumentException();
}

来源:@Emory

【讨论】:

  • 不错:) 欺骗系统的方法xD
  • 正是——查找表是您真正需要的。对于long 结果,级联的大小将翻倍。
  • 我认为您自己计算了 12 次。这就是我们编写代码的原因,以防止做乏味的事情:D(或者您是否编写代码来计算它?)
  • @RobAu 原因是每次用户要求阶乘时,运行计算的案例太少了
【解决方案4】:

这是我的节目

class FactorialUsingFor
{
    public static void main(String arg[])
    {
        int factorial = 1;
        int number = 6;

        for(int i = 1; i <= number; i++)
        {
            factorial *= i;
        }

        System.out.println("Factorial of number " + number + " is " + factorial);

    }
}

【讨论】:

    【解决方案5】:
    int sum = 1;
    
    for (int i = 2;i <= num;i++) {
        sum = sum * i;
    }
    

    【讨论】:

      【解决方案6】:

      试试这个程序:=

      import java.util.*;
      public class Factorials {
       public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        int i,num,a,b;
        int c=1;
        System.out.println("Enter the no.= ");
        num=sc.nextInt();
        for(i=1;i<=num;i++){
         c=i*c;
        }
        System.out.println("Factorial is "+c);
       }
      }
      

      【讨论】:

        【解决方案7】:
        import java.util.Scanner;
        public class Factorial {
            public static void main(String[] args){
                Scanner scn = new Scanner(System.in);
                System.out.println("Enter the number to find factorial");
                int o=scn.nextInt();
                int count=0,b=o-1,res=(o*(o-1)),r=0;
                while(count!=1)
                {
                 count=b-1;
                 r=count*res;
                 res=r;
                 r=1;
                 b=b-1;
                }
                System.out.println(res);
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-11-13
          • 1970-01-01
          • 2016-01-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多