e心e意

导航

C#语句(if、switch……case、do……while等)

switch……case……break

namespace jiayou2
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int x = 1; x < 6; x++)
            {
                switch (x)//计算变量X的值
                {
                    case 2://如果x=2;
                        Console.WriteLine("x is {0}--In Case 2",x);
                        break;//结束switch标记
                    case 5://如果x=5;
                        Console.WriteLine("x is {0}--In Case 5",x);
                        break;//结束switch标记
                    default :
                        Console.WriteLine("x is {0}--In Default case",x);
                        //如果x既不是2,也不是5
                        break;//结束switch标记
                }
            }
        }
    }
}

do……while

namespace do和while的循环
{
    class Program
    {
        static void Main(string[] args)
        {
            int x = 0;
            do
                Console.WriteLine ("x is {0}",x++);
               while(x<3);//条件
        }
    }
}

continue语句

namespace continue语句
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int x = 0; x < 5; x++)
            {
                if (x < 3)
                    continue;
                //当x>=3时执行下面语句
                Console.WriteLine("value of x is{o}",x);
            }
            /*
             结果展示
             value of x is 3
             value of x is 4             
             */
        }
    }
}

using语句

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;//using指令不是using语句

namespace using语句的使用
{
    class Program
    {
        static void Main(string[] args)
        {
            using (TextWriter tw = File.CreateText("Lincoln.txt"))//using语句
            {
                tw.WriteLine("Four score and seven years ago,");
            }
            using (TextReader tr = File.OpenText("Lincoln.txt"))//using语句
            {
                string InputString;
                while (null != (InputString = tr.ReadLine()))
                    Console.WriteLine(InputString );
            }
        }
    }
}

using语句多资源嵌套

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;//using指令

namespace using语句多个资源嵌套
{
    class Program
    {
        static void Main(string[] args)
        {
            using (TextWriter tw1 = File.CreateText("Lincoln.txt"),
                tw2 = File.CreateText("Franklin.txt"))//using语句多资源嵌套
            {
                tw1.WriteLine("Four score and seven years ago,");
                tw2.WriteLine("early to by bed;Early to rise^");
            }
            using (TextReader tr1 = File.OpenText("Lincoin.txt"),
                tr2 = File.OpenText("Franklin.txt"))//using语句多资源嵌套
            {
                string InputString;
                while (null != (InputString = tr1.ReadLine()))
                    Console.WriteLine(InputString );
                while (null != (InputString = tr2.ReadLine()))
                    Console.WriteLine(InputString );
            }
        }
    }
}

 

posted on 2014-05-17 21:38  e心e意  阅读(766)  评论(0)    收藏  举报