计算一到十的阶乘java
时间: 2023-11-29 15:42:19 浏览: 74
Java 编写的1到10的阶乘和
5星 · 资源好评率100%
可以使用循环来计算一到十的阶乘,Java代码如下:
```
public class Main {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
int factorial = 1;
for (int j = 1; j <= i; j++) {
factorial *= j;
}
System.out.println(i + "! = " + factorial);
}
}
}
```
输出结果为:
```
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40320
9! = 362880
10! = 3628800
```
阅读全文