
编写Java程序,实现接受用户输入的正整数,输出该数的阶乘.例如,输入数据4,则输出4!=1x2x3x4=24 5
2个回答
展开全部
直接贴代码:
public class Main {
public static void main(String[] args) throws IllegalAccessException {
System.out.println(factorial(3));
}
public static String factorial(long num) throws IllegalAccessException {
if (num < 0) {
throw new IllegalAccessException("输入参数非法");
}
StringBuilder builder = new StringBuilder();
builder.append(num).append("!=");
long temp = 1;
for (long i = 1; i <= num; i++) {
temp *= i;
builder.append(i).append("x");
}
builder.deleteCharAt(builder.length() - 1);
builder.append("=").append(temp);
return builder.toString();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询