简体   繁体   English

无法获得此“ Fatorial”程序的预期输出?

[英]can't get expected output to this “Factorial” program?

My name is sam and i'm a beginner in java.I have actually tried this "factorial" program on my own and i can't see whats going wrong with logic....I'm not getting the expected output,can anyone tell me why? 我的名字叫sam,我是java的初学者。我实际上是自己尝试了这个“阶乘”程序,我看不到逻辑出了什么问题。...我没有得到预期的输出,可以有人告诉我为什么吗? Thanks in advance,grt day to all the users :) 在此先感谢所有用户:)

Factorial: 阶乘:

import java.util.*;

class factorial {

    public static void main(String[] args) {

        int i;

        System.out.println("enter a number");

        Scanner in=new Scanner(System.in);

        int n=in.nextInt();

        int x=n;

        while(x!=0) {
            x=x*(n-1);
            n--;
        }

        System.out.println(x);

    }

}

When I enter (for example) 5, I expect to get the factoial of 5 (120). 例如,当我输入5时,我希望得到5的事实值(120)。 Instead, I get zero. 相反,我得到零。 This happens for all inputs. 所有输入都会发生这种情况。

If you really want to learn, start running code in your head, line by line, such as with: 如果您真的想学习,请开始在您的脑海中逐行运行代码,例如:

int n = 5;

int x = n;
while (x != 0) {
    x = x * (n - 1);
    n--;
}

Start with a sheet of paper thus: 从一张纸开始:

  x  |  n
-----+-----
     |
     |
     |
     |

and, as you "execute" each line, write down the new values. 并且,当您“执行”每一行时,记下新值。 Do that and it will be immediately obvious where your problem lies. 这样做,您的问题所在将立即显而易见。 It will also make you a better developer. 它还将使您成为更好的开发人员。

Please do not look at the rest of this answer until you have done that, and tried to fix it. 不要看这个答案的其余部分,直到你已经做到了,并试图修复它。


Once you've understood the problem and (hopefully) fixed it, compare what you have with the solution below: 了解问题并(有希望)解决问题后,将您的问题与以下解决方案进行比较:

int n = 5;          // input value.

int x = 1;          // initial accumulator set to identity (n * 1 = n).
while (n != 0) {    // use all values from n down to 1 (inclusive).
    x = x * n;      // multiply accumulator by that value.
    n--;            // get next value.
}

int x=n; int x = n;

    while(x!=0) {
        x=x*(n-1);
        n--;
    }

The result will be 0 because your loop will work for ever until x=0, x get the value of 0 in your loop when n becomes 0, try n!=0 in while loop condition 结果将为0,因为您的循环将一直工作直到x = 0,x在n变为0时在循环中获得0的值,在while循环条件下尝试n!= 0

You simply need to change the While loop condition to while(n!=1), because in your case when n becomes 1 then x=x*(n-1); 您只需要将While循环条件更改为while(n!= 1),因为在您的情况下,当n变为1时,则x = x *(n-1); will result in x being multiplied with 0. 将导致x乘以0。

Try out this code: 试用以下代码:

 public static void main(String[] args)

{

    int i;

    System.out.println("enter a number");

    Scanner in=new Scanner(System.in);

    int n=in.nextInt();

    int x=1;

    while(n!=0)

    {

        x*=(n);
        n--;

    }

    System.out.println(x);

}

Your while loop is multiplying your answer by 0 when n=1, and we all know what multiplying by 0 does. 当n = 1时,您的while循环会将您的答案乘以0,我们都知道乘以0会做什么。

EDIT: My bad, edited code. 编辑:我的错误,已编辑的代码。

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

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