返回列表 發帖
  1. package wwe;

  2. import java.util.Scanner;

  3. public class JPA04 {
  4.         static Scanner k = new Scanner(System.in);
  5.         public static void main(String[] args) {
  6.                 // TODO 自動產生的方法 Stub
  7.                
  8.                 System.out.println("Input the number  n: ");
  9.                 int n=k.nextInt();
  10.                 System.out.println("Ans:"+s(n));
  11.         }
  12.         static int s(int n)
  13.         {
  14.                 if(n==1)
  15.                 {
  16.                         return 2;
  17.                 }
  18.                 else
  19.                 {
  20.                         return s(n-1)+2*n;
  21.                 }
  22.         }

  23.        
  24.        
  25. }
複製代碼

TOP

返回列表