返回列表 發帖
  1. package a;

  2. public class Ch03 {
  3.     static int total(int n)
  4.     {
  5.             if(n==1)
  6.                     return 1;
  7.             else
  8.                     return n+total(n-1);
  9.     }
  10.        
  11.        
  12.        
  13.        
  14.         public static void main(String[] args) {
  15.                 // TODO 自動產生的方法 Stub
  16.                 System.out.println("1+2+....+5="+total(5));
  17.                 System.out.println("1+2+....+101="+total(101));
  18.                 System.out.println("1+2+....+257="+total(257));
  19.        
  20.        
  21.         }

  22. }
複製代碼

TOP

返回列表