C#循環語句(EduCoder實訓題目)


外面正在下好大的雨啊,嘿嘿,我就是懶狗一條,太懶了。

第1關:while循環

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

namespace F1
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] array = { 1, 2, 6, 9, 4, 7, 13, 12, 5, 3, 8, 10, 11 };

            /********** Begin *********/
            int temp = 0;
            int i = 0;  //計數器i
            while (i < array.Length-1) {
            int j=0;    //計數器j
            while (j < array.Length - 1 - i) {
                if (array[j] > array[j + 1]) {
                                                //開始交換數值
                    temp = array[j + 1];
                    array[j + 1] = array[j];
                    array[j] = temp;
                }
                j++;
            }
            i++;
        }
            /********** End *********/

            int size = 0;
            while (size < array.Length)
            {
                Console.WriteLine(array[size]);
                size++;
            }

        }
    }
}
View Code

第2關:do循環

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

namespace F2
{
    class Program
    {
        static void Main(string[] args)
        {
            /********** Begin *********/
            int a = 1;

            do
            {
                Console.WriteLine(a);
                a = a + 1;
            } while (a <= 20);

            Console.ReadLine();
            /********** End *********/
        }
    }
}
View Code

第3關:for循環

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

namespace F3
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] array = { 1, 2, 6, 9, 4, 7, 13, 12, 5, 3, 8, 10, 11 };
            int temp = 0;

            /********** Begin *********/
            for (int i = 0; i < array.Length - 1; i++)
            {
                for (int j = 0; j < array.Length - 1 - i; j++)
                {
                    if (array[j] > array[j + 1])
                    {
                        //開始交換數值
                        temp = array[j + 1];
                        array[j + 1] = array[j];
                        array[j] = temp;
                    }
                }
            }
            /********** End *********/

            int size = 0;
            while (size < array.Length)
            {   //輸出排序后的數組
                Console.WriteLine(array[size]);
                size++;
            }
        }

    }
}
View Code

第4關:foreach循環

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

namespace F3
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] nameList = { "Jackie", "Quentin", "Anna", "Charlize", "Eva", "Audrey", "Aoisola" };

            /********** Begin *********/
            Console.WriteLine("Now start naming");
            foreach (var v in nameList)
            {
                //執行遍歷操作
                Console.WriteLine(v);
            }
            Console.WriteLine("Great. Let's start");
            /********** End *********/
        }
    }
}
View Code

第5關:循環中斷

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

namespace F4
{
    class Program
    {
        static void Main(string[] args)
        {
            /********** Begin *********/
            for (int i = 1; i <= 9; i++)
            {
                for (int j = 1; j <= i; j++)
                {
                    if (i * j == 56)
                    {
                        Console.WriteLine("we found 56");
                        break;
                    }
                }
                Console.Write("\n");
            }
            /********** End *********/
        }
    }
}
View Code

本來這是昨天的實訓,最近太懶了,昨天就沒有及時發出來,今天才想起寫。


免責聲明!

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



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