- package wwe;
- import java.util.Scanner;
- public class JPA04 {
- static Scanner k = new Scanner(System.in);
- public static void main(String[] args) {
- // TODO 自動產生的方法 Stub
-
- System.out.println("Input the number n: ");
- int n=k.nextInt();
- System.out.println("Ans:"+s(n));
- }
- static int s(int n)
- {
- if(n==1)
- {
- return 2;
- }
- else
- {
- return s(n-1)+2*n;
- }
- }
-
-
- }
複製代碼 |