显示完整的计算以获得一个数字的阶乘(Java)

huangapple go评论71阅读模式
英文:

Display whole calculation for getting a factorial of a number (java)

问题

我有一个简单的Java程序,用来计算一个数字的阶乘。

运行程序后我得到的输出是:

> 5的阶乘是120

然而我希望生成如下格式的输出:

> 5 * 4 * 3 * 2 * 1 = 120

public static int calculateFactorial(int numInput) {
    
    int factorial = 1;
    
    for (int i = 1; i <= numInput; i++) {
        factorial = factorial * i;
    }
    
    return factorial;
}
英文:

I have a simple java program calculating the factorial of a number.

Output I get after running the program:

> the factorial of 5 is 120

However I would like to generate output looking like this:

> 5 * 4 * 3 * 2 * 1 = 120

public static int calculateFactorial(int numInput) {
    
    int factorial = 1;
    
    for (int i = 1; i <= numInput; i++) {
    factorial = factorial *i;
    }
    
    return factorial;
}

答案1

得分: 2

public static String calculateFactorialStr(int val) {
    String res = IntStream.range(0, val)
                          .map(i -> val - i)
                          .mapToObj(String::valueOf)
                          .collect(Collectors.joining(" * "));
    return res + " = " + calculateFactorial(val);
}

public static long calculateFactorial(int val) {
    long res = 1;

    for (int i = 2; i <= val; i++)
        res *= i;

    return res;
}
英文:
public static String calculateFactorialStr(int val) {
    String res = IntStream.range(0, val)
                          .map(i -&gt; val - i)
                          .mapToObj(String::valueOf)
                          .collect(Collectors.joining(&quot; * &quot;));
    return res + &quot; = &quot; + calculateFactorial(val);
}

public static long calculateFactorial(int val) {
    long res = 1;

    for (int i = 2; i &lt;= val; i++)
        res *= i;

    return res;
}

答案2

得分: 1

Static way:

public static String calculateFactorial(int numInput) {
    StringBuilder res = new StringBuilder();
    int factorial = 1;

    for (int i = 1; i <= numInput; i++) {

        res.append(String.valueOf(i));
        if (i != numInput) {
            res.append(" * ");
        }
        factorial = factorial * i;
    }
    res.append(" = ").append(factorial);

    return res.toString();
}

Object oriented way:

import java.util.ArrayList;
import java.util.List;

public class Factorial {
    private int number;
    private int result;

    public Factorial(int numInput) {
        number = numInput;
        int factorial = 1;

        for (int i = 1; i <= numInput; i++) {
            factorial = factorial * i;
        }
        result = factorial;
    }

    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        for (int i = 0; i <= number; i++) {

            res.append(i);
            if (i != number) {
                res.append(" * ");
            }
        }
        res.append(" = " + result);

        return res.toString();
    }
}

Then you can use a System.out.println(new Factorial(n)); to print the result

英文:

Static way:
Create your string in you function (this does not operate with n < 0) :

public static String calculateFactorial(int numInput) {
        StringBuilder res = new StringBuilder();
        int factorial = 1;

        for (int i = 1; i &lt;= numInput; i++) {

            res.append(String.valueOf(i));
            if (i != numInput) {
                res.append(&quot; * &quot;);
            }
            factorial = factorial *i;
        }
        res.append(&quot; = &quot;).append(factorial);

        return res.toString();
}

Object oriented way:
You can also create a Factorial class if you know about object oriented in Java (this does not operate with n < 0) :

import java.util.ArrayList;
import java.util.List;

public class Factorial {
    private int number;
    private int result;

    public Factorial(int numInput) {
        number = numInput;
        int factorial = 1;

        for (int i = 1; i &lt;= numInput; i++) {
            factorial = factorial * i;
        }
        result = factorial;
    }

    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        for (int i = 0; i &lt;= number; i++) {

            res.append(i);
            if (i != number) {
                res.append(&quot; * &quot;);
            }
        }
        res.append(&quot; = &quot; + result);

        return res.toString();
    }
}


Then you can use a System.out.println(new Factorial(n)); to print the result

答案3

得分: 0

public static String calculateFactorial(int numInput) {
    int factorial = 1;
    String calc = "";

    for (int i = 1; i <= numInput; i++) {
        if (i != 1) {
            calc += " * ";
        }
        calc += i;
        factorial = factorial * i;
    }
    calc += " = " + factorial;
    return calc;
}

或者如果您喜欢 x * (x-1) * ... * 1

public static String calculateFactorial(int numInput) {

    int factorial = 1;
    String calc = "";

    for (int i = numInput; i > 0; i--) {
        if (i != numInput) {
            calc += " * ";
        }
        calc += i;
        factorial = factorial * i;
    }
    calc += " = " + factorial;
    return calc;
}
英文:
public static String calculateFactorial(int numInput) {
    int factorial = 1;
    String calc = &quot;&quot;;

    for (int i = 1; i &lt;= numInput; i++) {
      if (i != 1) {
        calc += &quot; * &quot;;
      }
    calc += i;
    factorial = factorial * i;
    }
    calc += &quot; = &quot; + factorial;
    return calc;
}

or, if you prefer x * (x-1) * ... * 1

  public static String calculateFactorial(int numInput) {
    
    int factorial = 1;
    String calc = &quot;&quot;;

    for (int i = numInput; i &gt; 0; i--) {
      if (i != numInput) {
        calc += &quot; * &quot;;
      }
    calc += i;
    factorial = factorial * i;
    }
    calc += &quot; = &quot; + factorial;
    return calc;
}

huangapple
  • 本文由 发表于 2020年10月9日 19:26:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/64279125.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定