Guest User

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

a guest
Apr 8th, 2016
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.64 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.             int result = 0;
  23.             for (int i = 1; i <=x; i++)
  24.             {
  25.                 result = result + i;
  26.             }
  27.             return result;
  28.         }
  29.  
  30.     }
  31.  
  32. }
Advertisement
Add Comment
Please, Sign In to add comment