Guest User

印出1到5累加結果(不用FOR)

a guest
Apr 8th, 2016
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.69 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace C_Sharp空白練習檔
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Calculator c = new Calculator();
  14.             int result = c.SumFrom1ToX(5);
  15.             Console.WriteLine(result);
  16.         }
  17.     }
  18.     class Calculator
  19.     {
  20.         public int SumFrom1ToX(int x)
  21.         {
  22.             if(x==1)
  23.             {
  24.                 return 1;
  25.             }
  26.             else
  27.             {
  28.                 int result = x + SumFrom1ToX(x - 1);
  29.                 return result;
  30.             }
  31.  
  32.  
  33.         }
  34.  
  35.     }
  36.  
  37. }
Advertisement
Add Comment
Please, Sign In to add comment