C#函數(EduCoder實訓題目)


第1關:寫一個函數

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace G1
{
    class Program
    {
        static void Main(string[] args)
        {
            string myWords = Hello();
            Console.WriteLine(myWords);
        }

        /********** Begin *********/
        static string Hello()
        {
            return "Hello World, this is my function!";
        }
        /********** End *********/

    }
}
View Code

第2關:迭代函數

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace G2
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] a = { 3, 4, 33, 2, 121, 34, 65, 34 };
            int size = a.Length - 1;
            int result = Max(a, size);
            Console.WriteLine(result);
        }

        /********** Begin *********/
         static int Max(int[] array, int i)
        {
            int temp;
            if (i == 0)
            {
                return array[0];
            }
            else
            {
                temp = Max(array, i - 1);
                if (temp > array[i])
                {
                    return temp;
                }
                else
                {
                    return array[i];
                }
            }
        }
        /********** End *********/
    }
}
View Code

第3關:變量的作用域

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace G3
{
    class Program
    {

        static void Main(string[] args)
        {
            outputDiscount();
        }
        /********** Begin *********/
        static int originalPrice = 100;
        static float discount = 0.73F;
        static void outputDiscount()
        {
            Console.WriteLine("After discounting, the price is " + originalPrice * discount);
        }
        /********** End *********/

    }
}
View Code

第4關:引用參數和值參數

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace G4
{
    class Program
    {
        static void Main(string[] args)
        {
            int totalPrice = 100; ;
            /********** Begin *********/
            int originalPrice = 100;
            float discount = 0.73F;
            /********** End *********/

            outputDiscount(originalPrice, discount, ref totalPrice);
            Console.WriteLine("main totalPrice is " + totalPrice);
        }
        /********** Begin *********/
        static void outputDiscount(int orgPrice, float dis,ref int total)
        {
            total = (int)(orgPrice * dis);
            Console.WriteLine("After discounting, the price is " + total);
        }
        /********** End *********/
    }
}
View Code


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM