小区轻微刮蹭逃逸时效:JAVA高手来帮个小忙...
来源:百度文库 编辑:神马品牌网 时间:2024/10/03 02:53:07
2.编写计算X的n次的递归方法
import java.util.Scanner;
public class Test{
public static void main(String args[]){
int num = 0;
int sum = 0;
System.out.println("please enter a number between 1 and 9:");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
num = n;
for(int i = 1; i <= n; i++){
sum += num;
num = 10*num + n;
}
System.out.println("the result is " +sum);
}
}
另一个
import java.util.Scanner;
public class Test2 {
public static long calc(int num, int n){
if(n == 0)
return 1;
return (long)(num * calc(num, n - 1));
}
public static void main(String[] args) {
int num = 0;
int n = 0;
System.out.println("please enter a number:");
Scanner sc = new Scanner(System.in);
num = sc.nextInt();
System.out.println("please enter a number:");
n = sc.nextInt();
System.out.println("the result is " + Test2.calc(num, n));
}
}
其中的类名可以自己改
可不可以用其它语言做一个, 比方说c,vb,vf, 偏java没太做过