您的位置:首页 > 编程语言 > C#

体验C#——关于一些循环语句

2014-12-02 10:15 323 查看
简单写了个C#关于循环语句测试的程序:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace XunhuanTest
{
    //for语句
    class ForStatement
    {
        public void forTest()
        {
            //打印直角三角形
            for (int i = 0; i < 10; i++)
            {
                for (int j = 0; j < i; j++)
                {
                    Console.Write("*");
                }
                Console.WriteLine();
            }
            //Console.ReadKey();
        }
    }
    //while语句
    class WhileStatement
    {
        //打印直角三角形
        public void whileTest()
        {
            int i = 0;
            while (i < 10)
            {
                int j = 0;
                while (j < i)
                {
                    Console.Write("*");
                    j++;
                }
                Console.WriteLine();
                i++;
            }
        }
        //do-while和while的区别
        public void dowhile_Or_while()
        {
            int i = 16;
            //do_while
            do
            {
                Console.WriteLine("dowhile 的输出结果:{0}",i);
            }while(i<15);
            //while
            while(i<15)
            {
                Console.WriteLine("while 的输出结果:{0}",i);
            }
        }

        
    }
    //foreach语句
    class ForeachStatement
    {
        public void foreachTest()
        {
            string[] str = { "sdfd", "wefe" };
            foreach (string  i in str)
            {
                Console.WriteLine(i);
            }
        
    }
    }
    class Program
    {
        static void Main(string[] args)
        {
            //实例化一个for测试类
            Console.WriteLine("测试for语句");
            ForStatement forStatement = new ForStatement();
            forStatement.forTest();
            //实例化一个foreach测试类
            Console.WriteLine("测试foreach语句");
            ForeachStatement foreachStatement = new ForeachStatement();
            foreachStatement.foreachTest();
            //实例化一个while测试类
            Console.WriteLine("测试while语句");
            WhileStatement whileStatement = new WhileStatement();
            whileStatement.whileTest();
            whileStatement.dowhile_Or_while();//看while和do-while的区别
            Console.ReadKey();
        }
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: