返回列表 發帖
本帖最後由 郭又瑄 於 2023-9-15 19:41 編輯
  1. import java.util.Scanner;
  2. public class Ch01 {

  3.         public static void main(String[] args) {
  4.                 // TODO 自動產生的方法 Stub
  5.                 Scanner sc=new Scanner(System.in);

  6.                 int n;
  7.                 try{
  8.                         n=sc.nextInt();
  9.                         if(n==0){
  10.                                 System.out.println("error");
  11.                                 return;
  12.                         }
  13.                         n=Math.abs(n);
  14.                         System.out.println(compute(n));
  15.                 }catch(Exception e){
  16.                         System.out.println("error");
  17.                         return;
  18.                 }
  19.                 public static int compute(int i){
  20.                         if(i==1)
  21.                                 return 2;
  22.                         else
  23.                                 return compute(i-1)+3*i;
  24.                 }
  25.         }
複製代碼

TOP

返回列表