简体   繁体   English

java中使用for循环的阶乘

[英]factorial in java using for loop

 /* this program 
  * finds the factorial for any number
  */

public class forLoop1{

    public static void main(int x){    
        int init; 
        for( init = x; init < 2; init--){
            int finalint = init * --init; 
            System.out.println(finalint); 
        }
    }
}

^^ This program doesn't have an output, can anyone think of what's going wrong here? ^^ 这个程序没有输出,谁能想到这里出了什么问题? Any help would be appreciated, thank you!任何帮助将不胜感激,谢谢!

If you want to use command line argument then parse String to int .如果要使用命令行参数,则将String解析为int

public static void main(String args[]) {
    int n=Integer.parseInt(args[0]);
    int fac = 1;
    for(int i = n; i >= 2; i--) {
        fac = fac * i;
    }
    System.out.println(fac);
}

You should run this program as java forLoop1 5 for an input 5 for example.例如,对于输入5 ,您应该将此程序作为java forLoop1 5运行。

You have problems at -- ummmm -- almost each line.你在——嗯——几乎每一行都有问题。 Have a look at the code below and trace each step with n = 4 (or any other number).看看下面的代码,并用 n = 4(或任何其他数字)跟踪每个步骤。

public class forLoop1{
  public static void main(String[] args){    
    // n is the number whose factorial is to be calculated
    int n = 10; 
    int factorial = 1;
    for(int i = n; i >= 2; i--){
      factorial = factorial * i;
    }
    System.out.println(factorial);
  }
}
import java.util.Scanner;

public class forLoop1{

 public static void main(String args[])
  { 
  System.out.println("Enter a number greater than zero.");
  Scanner in = new Scanner(System.in);

  int n = in.nextInt();
  in.close();

  int fact = 1;
  for (n = n; n>=2; n--)
    fact *= n;

  System.out.println("Factorial of "+n+" is = "+fact);
  }
}

This should work.这应该有效。 Scanner will read user input.扫描仪将读取用户输入。

In your code, the loop will only execute if init is less than 2 to start with.在您的代码中,循环只会在 init小于2 时执行。

Here is an explanation of the errors in your code:以下是对代码中错误的解释:

public static void main(int x){ //Should be public static void main(String[] args)       
    int init; 
    for( init = x; init < 2; init--){ //Should be init >= 2
        int finalint = init * --init; //Possibly should be init * (init - 1)
                                      //In each iteration of the loop,      
                                      //finalint will be overwritten
    System.out.println(finalint); //This line is fine.
}

Thanks for the answers!感谢您的回答! Arjun, great your logic works perfectly A few things though: I do not need the (String[] args) part; Arjun,很棒,你的逻辑很完美 不过有几件事:我不需要(String[] args)部分; I mentioned before too, as my IDE takes input for the x value by itself.我之前也提到过,因为我的 IDE 自己接受x值的输入。 So this is the code I used in the end:所以这是我最后使用的代码:

  /* Factorial 
   * program
   */


public class forLoop2 {

    public void main(long x) { // long x is correct, as my IDE takes input through this 
        long fact = 1; 

        for( x = x; x >= 2; x--) 
            fact = fact * x; 

        System.out.println(fact); 
    }
}
import java.util.*;

public class forLoop1{
    public static void main(String args[]){ 

        Scanner input=new Scanner(System.in);
        System.out.println("Enter the number:");
        int x=input.nextInt();

        int finalint=1;
        for( int init=1;init<=x;init++){
            finalint=finalint*init;
        }
    }
}

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

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