Java编写程序,求1!-2!+3!-4!+5!-6!
时间: 2023-11-28 08:44:05 浏览: 99
wx306小说实体书商城-ssm+vue+uniapp.zip(可运行源码+sql文件+文档)
以下是Java编写程序,求1!-2!+3!-4!+5!-6!的代码:
```java
public class FactorialSum {
public static void main(String[] args) {
int n = 6;
int sum = 0;
int sign = 1;
int factorial = 1;
for (int i = 1; i <= n; i++) {
factorial *= i;
sum += sign * factorial;
sign = -sign;
}
System.out.println("1!-2!+3!-4!+5!-6! = " + sum); // 输出:1!-2!+3!-4!+5!-6! = -11
}
}
```
阅读全文