英文:
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 -> 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;
}
答案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 <= 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:
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 <= 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
答案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 = "";
for (int i = 1; i <= numInput; i++) {
if (i != 1) {
calc += " * ";
}
calc += i;
factorial = factorial * i;
}
calc += " = " + factorial;
return calc;
}
or, if you prefer 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;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论