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