day03 c#簡單的函數

數學函數。包括判斷偶數,打印數組,求數組最大值,冒泡排序和水仙花 數。

 

 

 

 public class MathTool
    {
        public bool isEvenNumber(int i)     //是否是偶數
        {
            Console.WriteLine("判斷數是不是偶數");
            if (i % 2 == 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public void printf(int[] i)     //打印數組
        {
            Console.WriteLine("打印數組");
            for (int j = 0; j < i.Length; j++)
            {
                Console.Write(i[j] + " ");
            }
        }
        public void maxInArray(int[] i)    //找出數組最大值
        {
            Console.WriteLine("找出數組最大值");
            int max = 0;
            for (int j = 0; j < i.Length; j++)
            {
                if (i[j] > max)
                {
                    max = i[j];
                }
            }
            Console.Write("數組最大值爲: " + max);
        }
        public void bubbleSort(int[] array)       //冒泡排序
        {
            for (int i = 0; i < array.Length; i++)
            {
                bool hasExchange = false;
                for (int j = 0; j < array.Length - i - 1; j++)
                {
                    if (array[j] > array[j + 1])
                    {
                        hasExchange = true;
                        int temp = array[j];
                        array[j] = array[j + 1];
                        array[j + 1] = temp;
                    }
                }
                if (!hasExchange)
                {
                    break;
                }
                for (int m = 0; m < array.Length; m++)
                {
                    Console.Write(array[m] + "\t");
                }
                Console.WriteLine();
            }
            Console.WriteLine("排序結束๳");
            for (int m = 0; m < array.Length; m++)
            {
                Console.Write(array[m] + "\t");
            }
        }
        public void narcNumber() 
        {
            int hun, ten, ind;                           //水仙花數
            for (int i = 100; i < 1000; i++)
            {
                hun = i / 100;
                ten = i % 100 / 10;
                ind = i % 10;
                if (hun * hun * hun + ten * ten * ten + ind * ind * ind == i)
                { Console.WriteLine(i); }
            }
        }

    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章