返回列表 發帖
  1. import java.util.Scanner;


  2. public class Ch01 {
  3.         static int total(int n){
  4.                 if(n == 1)
  5.                         return 1;
  6.                 else
  7.                         return n + total(n - 1);
  8.         }
  9.        

  10.         public static void main(String[] args) {
  11.                 // TODO 自動產生的方法 Stub
  12.                 System.out.println("1+2+......+5="  + total(5));
  13.                 System.out.println("1+2+......+101="  + total(101));
  14.                 System.out.println("1+2+......+257="  + total(257));
  15.         }
  16. }
複製代碼

TOP

返回列表