使用循环计算阶乘数,Java

使用循环计算阶乘数,Java,java,loops,factorial,Java,Loops,Factorial,我试图计算7阶乘的值并显示答案,但当我试图查找一种方法来实现这一点时,我一直在寻找编写的代码,以便首先必须从用户输入一个数字,然后它会将用户输入的任何数字作为因子。但我已经知道我需要什么号码了,很明显,所以代码会有所不同,我很难弄清楚如何做到这一点 我一开始试过这个 public class Ch4_Lab_7 { public static void main(String[] args) { int factorial = 7; while

我试图计算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--;
        }
    }
}
public class Ch4\u Lab\u 7
{
公共静态void main(字符串[]args)
{
整数阶乘=7;
while(阶乘0)
System.out.println(阶乘*事实…
阶乘--;
}
}
}
但它所做的只是显示7*7,然后是6*6,然后是5*5,等等,这不是我想要做的。
有人知道如何正确操作吗?

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

public static void main(String[] args)
    {
        int input = 7;
        int factorial = 1;
        while (input > 0)
        {
          factorial = factorial * input
          input--;
        }
        System.out.println("Factorial = " + factorial);
    }
import java.util.Scanner;
公共类阶乘{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
//提示
System.out.print(“输入一个数字以查找其阶乘”);
//输入要运行的时间
int number=input.nextInt();
//声明新的int
整数因子=1;
//运行循环并在每次运行时乘以系数
因为(inti=1;i1)原因:
您看到的方法可能是递归的,您似乎已经对其进行了编辑

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

试试这个

//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的阶乘。

您应该创建一个
公共静态int阶乘(int n)
函数只计算其参数的阶乘
n
;也可以通过谷歌搜索
阶乘java
得到有趣的结果。只是一个提示:您需要两个变量——一个用于循环变量,一个用于结果。我建议您在纸上写下您希望它做的事情。除非您对编程非常陌生,否则这应该在你的理解。我不会把结果称为“总计”,这让我想到的是总和,而不是乘积(这个名字让人类读者感到困惑)。为什么不将其称为
结果
事实
?提供的代码可能会有所帮助,但为了使其成为一个好的答案,您还应该描述/解释您的代码解决问题的确切原因/方式。可以实现扫描仪,而不是在第4行使用5!来获取用户的输入。关于代码的工作方式,请参阅代码注释。
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;
}
 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);
 }
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. */