繁体   English   中英

使用循环计算阶乘数,Java

[英]Using loops to compute factorial numbers, Java

我正在尝试计算7阶乘的值并显示答案,但是当我尝试寻找一种方法来执行此操作时,我一直在寻找编写的代码,因此必须首先从用户输入一个数字,然后再输入它会考虑用户输入的数字。但是很明显,我已经知道我需要多少数字,因此代码将有所不同,并且我很难确定如何执行此操作。

我一开始尝试了

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--;
        }
    }
}

但是它所做的只是显示7 * 7,然后是6 * 6,然后是5 * 5,依此类推,这不是我想要的。 有人知道如何正确地做吗?

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);
    }
}

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

输入:5输出:120

输入:7输出:5040

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

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

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

第二:无论何处,您都不会存储阶乘的时间结果。

尝试这个

//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);

尝试这个:

 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);
 }

这将打印出阶乘为7。

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. */

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM