6个回答
展开全部
import java.util.ArrayList;import java.util.List;
import java.util.Scanner;
public class Test { public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("please enter a num : ");
int num = scan.nextInt();
System.out.println("factorial : " + compute(num,"*"));
System.out.println("sum : " + compute(num,"+"));
}
public static int m3(int num,String sign){
if(num == 1){
return num;
}
return sign.charAt(0) == '+' ? num + m3(num - 1,sign) : sign.charAt(0) == '*' ? num * m3(num - 1,sign) : 0;
}
public static long compute(int num,String sign){
long res = 1;
if (sign.charAt(0) == '+') {
for (int i = 1; i <= num; i++) {
res += m3(i,sign);
}
res --;
}else if(sign.charAt(0) == '*'){
for (int i = 1; i <= num; i++) {
res *= m3(i,sign);
}
}else{
return -1;
}
return res;
}
}
import java.util.Scanner;
public class Test { public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("please enter a num : ");
int num = scan.nextInt();
System.out.println("factorial : " + compute(num,"*"));
System.out.println("sum : " + compute(num,"+"));
}
public static int m3(int num,String sign){
if(num == 1){
return num;
}
return sign.charAt(0) == '+' ? num + m3(num - 1,sign) : sign.charAt(0) == '*' ? num * m3(num - 1,sign) : 0;
}
public static long compute(int num,String sign){
long res = 1;
if (sign.charAt(0) == '+') {
for (int i = 1; i <= num; i++) {
res += m3(i,sign);
}
res --;
}else if(sign.charAt(0) == '*'){
for (int i = 1; i <= num; i++) {
res *= m3(i,sign);
}
}else{
return -1;
}
return res;
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询