使用循环计算阶乘数,Java

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18907195/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 12:12:19  来源:igfitidea点击:

Using loops to compute factorial numbers, Java

javaloopsfactorial

提问by Dariani Disani

I'm trying to compute the value of 7 factorial and display the answer, but when I tried to look up a way to do this I kept finding code that was written so that a number first had to be put in from the user and then it would factor whatever number the user put in. But I already know what number I need, obviously, so the code is going to be different and I'm having trouble figuring out how to do this.

我正在尝试计算 7 阶乘的值并显示答案,但是当我尝试查找一种方法时,我一直在寻找编写的代码,以便首先必须从用户输入一个数字,然后它会考虑用户输入的任何数字。但我已经知道我需要什么数字,很明显,所以代码会有所不同,我很难弄清楚如何做到这一点。

I tried this at first

我一开始试过这个

public class Ch4_Lab_7 
{
    public static void main(String[] args)
    {
        int factorial = 7;
        while (factorial <= 7)
        {
        if (factorial > 0)
        System.out.println(factorial*facto… 
        factorial--;
        }
    }
}

But all it does is display 7*7, then 6*6, then 5*5, and so on, and this isn't what I'm trying to do. Does anyone know how to do it correctly?

但它所做的只是显示 7*7,然后是 6*6,然后是 5*5,等等,这不是我想要做的。有谁知道如何正确地做到这一点?

回答by Juned Ahsan

You need to have two variables, one for the factorial calculation and other for the purpose of counter. Try this, i have not tested it but should work:

您需要有两个变量,一个用于阶乘计算,另一个用于计数器。试试这个,我还没有测试过,但应该可以工作:

public static void main(String[] args)
    {
        int input = 7;
        int factorial = 1;
        while (input > 0)
        {
          factorial = factorial * input
          input--;
        }
        System.out.println("Factorial = " + factorial);
    }

回答by Noob

import java.util.Scanner;

public class factorial {
    public static void main (String[] args) {
        Scanner input = new Scanner(System.in);
        //Gives Prompt
        System.out.print("Enter a number to find the factorial of it");
        //Enter the times you want to run
        int number = input.nextInt();
        //Declares new int    
        int factor = 1;
        //Runs loop and multiplies factor each time runned     
        for (int i=1; i<=number; i++) {
            factor = factor*i;
        }
        //Prints out final number
        System.out.println(factor);
    }
}

Just keep multiplying it and until it reaches the number you inputted. Then print.

只需继续乘以它,直到它达到您输入的数字。然后打印。

Input:5 Output:120

输入:5 输出:120

input:7 Output:5040

输入:7 输出:5040

回答by Sinn

1st reason: The methods you seen are probably recursive, which you seem to have edited.

第一个原因:您看到的方法可能是递归的,您似乎已经对其进行了编辑。

2nd: You are not storing, ANYWHERE the temporal results of factorial.

第二:您没有在任何地方存储阶乘的时间结果。

Try this

尝试这个

//number->n for n!
int number = 7;
//We'll store here the result of n!
int result = 1;
//we start from 7 and count backwards until 1
while (number > 0) {
    //Multiply result and current number, and update result
    result = number*result;
    //Update the number, counting backwards here
    number--;
}
//Print result in Screen
System.out.println(result);

回答by Prince

int a=7, fact=1, b=1;
do
{
    fact=fact*b;//fact has the value 1  as constant and fact into b will be save in fact to multiply again. 
    System.out.print(fact);
    b++;
}
while(a>=b); // a is greater and equals tob.

回答by TheCoffeeCup

Try this:

尝试这个:

 public static void main(String args[]) {
      int i = 7;
      int j = factorial(i); //Call the method
      System.out.println(j); //Print result
 }

 public static int factorial(int i) { //Recursive method
      if(i == 1)
           return 1;
      else
           return i * factorial(i - 1);
 }

This would print out the factorial of 7.

这将打印出 7 的阶乘。

回答by Immers Cherub

public class Factorial {

public static void main(String[] args) {
    int result = factorial(5); //this is where we do 5!, to test.
    System.out.println(result);

}

public static int factorial(int n) {
    int x = 1;
    int y = 1;
    for (int i = 1; i <= n; i++) {

        y = x * i;
        x = y;

    }
    return y;
}

}

}

/*so, for 3! for example, we have:
i=1:
y = x * i, where x = 1, so that means:
y = 1*1 ; y= 1; x = y so x = 1. Then i increments =>
i = 2:
y = x * i. x is 1 and i is 2, so we have y = 2. Next step in code: x=y, means x = 2. Then i increments =>
i = 3:
y = x *i so we have y = 2*3. y=6. */