返回列表 發帖
  1. package homework;

  2. import java.util.Scanner;

  3. public class Main {

  4.         public static void main(String[] args) {

  5.                 Scanner sc = new Scanner(System.in);
  6.                 int n;

  7.                 try {
  8.                         n=sc.nextInt();
  9.                         if(n<0)
  10.                                 System.out.print(compute(Math.abs(n)));
  11.                         else if(n==0)
  12.                                 System.out.print("error");
  13.                         else
  14.                                 System.out.print(compute(n));

  15.                 } catch (Exception e) {
  16.                         System.out.print("error");
  17.                         return;
  18.                 }
  19.                 // TO DO

  20.         }

  21.         public static int compute(int i) {
  22.                 if(i==1)
  23.                         return 2;
  24.                 return compute(i-1)+3*i;

  25.         }
  26. }
複製代碼

TOP

返回列表