返回列表 發帖
  1. package text;
  2. import java.util.Scanner;
  3. public class JPA04 {
  4.     static Scanner keyboard = new Scanner(System.in);
  5.     public static void main(String args[]) {
  6.         
  7.       System.out.print("Input the number n: ");
  8.       int num=keyboard.nextInt();
  9.       System.out.println("Ans: "+f(num));
  10.     }
  11.    
  12.     static int f(int n)
  13.     {
  14.             if(n==1)
  15.             {
  16.                     return 2;
  17.             }
  18.             else
  19.                     return f(n-1)+2*n;
  20.     }
  21.    

  22. }
複製代碼

TOP

返回列表