编写求K!(K的阶乘)的函数,再调用该函数求1!+3!+5!+...+N!之和并输出
说明
l)阶乘算法:K!=1*2*3…..*K
2)要求编写一函数,此函数的作用是:输入一个正整数,输出为此数对应的阶乘值
3)求和的最大数N需由用户处取得,如最大数N为偶数则输出到N-1的阶乘和,如N=6,和为:1!+3!+5!
需要实现步骤
1 、编写阶乘计算函数
2 、编写main 函数
1 )在屏幕上打印提示:“请输入要求和的阶乘的最大数”
2 )编写循环语句,并调用阶乘函数进行具体某个阶乘的计算,累加,最后输出。
3 )输入时需注意N的值必须大于零
3 、程序运行参考界面图如下所示:
说明
l)阶乘算法:K!=1*2*3…..*K
2)要求编写一函数,此函数的作用是:输入一个正整数,输出为此数对应的阶乘值
3)求和的最大数N需由用户处取得,如最大数N为偶数则输出到N-1的阶乘和,如N=6,和为:1!+3!+5!
需要实现步骤
1 、编写阶乘计算函数
2 、编写main 函数
1 )在屏幕上打印提示:“请输入要求和的阶乘的最大数”
2 )编写循环语句,并调用阶乘函数进行具体某个阶乘的计算,累加,最后输出。
3 )输入时需注意N的值必须大于零
3 、程序运行参考界面图如下所示:
static int count(int n){
if(n<=0){
return -1;
}
int result=0;
int finalResult=0;
while(n>1){
result=n*(n-1);
finalResult+=result;
n--;
}
return finalResult;
}
static void getSum(){
Scanner scanner=new Scanner(System.in);
int n=scanner.nextInt();
if(n<=0){
System.exit(0);
}
int sum=0;
for(;n>0;n-=2){
if(n%2==0){
n--;
sum+=count(n);
}else{
sum+=count(n);
}
}
System.out.println(sum);
}
static int count(int n){
if(n<=0){
return -1;
}
int result=0;
int finalResult=0;
if(n==1){
return 1;
}
while(n>1){
result=n*(n-1);
finalResult+=result;
n--;
}
return finalResult;
}
static void getSum(){
Scanner scanner=new Scanner(System.in);
int n=scanner.nextInt();
int sum=0;
if(n<=0){
System.exit(0);
}
else if(n==1){
sum=1;
}else{
for(;n>0;n-=2){
if(n%2==0){
n--;
sum+=count(n);
}else{
sum+=count(n);
}
}
}
System.out.println(sum);
}
请输入要求和的阶乘的最大数:1
奇项阶乘和:1
请输入要求和的阶乘的最大数:2
阶乘:2
请输入要求和的阶乘的最大数:3
奇项阶乘和:7
请输入要求和的阶乘的最大数:11111
奇项阶乘和: 回复内容过长!
请输入要求和的阶乘的最大数:end
成功生成(总时间:43 秒)
*/
import java.io.*;
import java.math.BigInteger;public class Main { public static void main(String[] args) throws Exception {
java.io.BufferedReader bi = new BufferedReader(new InputStreamReader(System.in));
String str;
System.out.print("请输入要求和的阶乘的最大数:");
while ((str = bi.readLine()) != null) {
if (str.equals("end")) {
System.exit(0);
} else {
BigInteger num = new BigInteger(str);
if (num.compareTo(BigInteger.ZERO) <= 0) {
System.out.println("输入数字应大于0");
System.out.print("请输入要求和的阶乘的最大数:");
continue;
}
try {
if (num.remainder(BigInteger.valueOf(2)).compareTo(BigInteger.valueOf(1)) == 0) {
System.out.println("奇项阶乘和:" + jcsum(num));
} else {
System.out.println("阶乘:" + jc(num));
}
} catch (Exception e) {
System.out.println("输入不是数字");
}
System.out.print("请输入要求和的阶乘的最大数:");
}
}
} private static BigInteger jcsum(final BigInteger ns) {
if (ns.compareTo(BigInteger.ZERO) <= 0) {
return BigInteger.valueOf(0);
}
BigInteger sum = BigInteger.valueOf(0);
BigInteger p = BigInteger.valueOf(1);
BigInteger ps = BigInteger.valueOf(1);//当前阶乘值
while (p.compareTo(ns) <= 0) {
ps = ps.multiply(p);
if (p.remainder(BigInteger.valueOf(2)).compareTo(BigInteger.valueOf(1)) == 0) {
sum = sum.add(ps);
}
p = p.add(BigInteger.ONE);
}
return sum;
} private static BigInteger jc(BigInteger n) {
if (n.compareTo(BigInteger.ZERO) <= 0) {
return BigInteger.valueOf(0);
}
BigInteger sum = BigInteger.valueOf(1);
while (n.compareTo(BigInteger.ZERO) > 0) {
sum = sum.multiply(n);
n = n.subtract(BigInteger.ONE);
}
return sum;
}
}
我看LZ上肯定没装eclipse或NetBeans才写个没介面滴