码迷,mamicode.com
首页 > Windows程序 > 详细

C#编程(3_流程控制)

时间:2014-10-26 07:58:53      阅读:349      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   ar   使用   for   sp   div   

  • 一个简单的Bool循环:
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Please Enter an integer:");
            int myInt=Convert.ToInt32(Console.ReadLine());
            bool isLessThan10 = myInt < 10;
            bool isBetweenThan0And5 = (myInt >= 0) && (myInt <= 5);
            Console.WriteLine("Integer is less than 10?{0}",isLessThan10);
            Console.WriteLine("Integer between 0 and 5?{0}",isBetweenThan0And5);
            Console.WriteLine("Exactly one of the above is true?{0}",isLessThan10^isBetweenThan0And5);//对两个变量执行逻辑异或操作
            Console.ReadKey();


        }
    }
}
  • 一个简单的if循环:
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string comparison;
            Console.WriteLine("Enter a number:");
            double var1 = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("Enter anther number:");
            double var2 = Convert.ToDouble(Console.ReadLine());
            if (var1 < var2)
                comparison = "less than";
            else
            {
                if (var1 == var2)
                    comparison = "equal to";
                else
                    comparison = "greater than";
            }
            Console.WriteLine("The frist number is {0} the second number.",comparison);
            Console.ReadKey();

        }
    }
  • 一个简单的Switch循环:
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            const string myName = "karli";
            const string sexyName = "angelina";
            const string sillyName = "ploppy";
            string name;
            Console.WriteLine("What is you name?");
            name = Console.ReadLine();
            switch (name.ToLower())//name.ToLower()是一个标准命令,用于处理所有输入的字符串,使输入的字符串与与给定字符串匹配。
            { 
                case myName:
                    Console.WriteLine("You have the same name with me!");
                    break;
                case sexyName:
                    Console.WriteLine("my,What a sexy name you have!");
                    break;
                case sillyName:
                    Console.WriteLine("That‘s a very ploppy name.");
                    break;
            }
            Console.WriteLine("Hello {0}!",name);
            Console.ReadKey();
        }
    }
}
  • 一个简单的do循环:
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //代码利用固定的利率,对年度计算余额的过程重复必要的次数,知道满足临界条件为止,在每次循环中,递增一个计数器变量,就可以确定需要多少年:
            double balance, interestRate, targerBalance;
            Console.WriteLine("What is your current balance?");
            balance = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("What is your current annual interest rate(in %)?");
            interestRate = 1 + Convert.ToDouble(Console.ReadLine())/100.0;
            Console.WriteLine("What balance would you like to have?");
            targerBalance = Convert.ToDouble(Console.ReadLine());
            int totalYears = 0;
            do
            {
                balance *= interestRate;
                ++totalYears;
            }
            while (balance < targerBalance);
            Console.WriteLine("In {0} year {1} you‘ll have the blance of {2}.",totalYears,totalYears==1?"":"s",balance);//三元运算符
            Console.ReadKey();
        }
    }
}
  • 使用While循环:
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            double balance, interestRate, targerBalance;
            Console.WriteLine("What is your current balance?");
            balance = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("What is your current annual interest rate(in %)?");
            interestRate = 1 + Convert.ToDouble(Console.ReadLine()) / 100.0;
            Console.WriteLine("What balance would you like to have?");
            targerBalance = Convert.ToDouble(Console.ReadLine());
            int totalYears = 0;
            while (balance < targerBalance)
            {
                balance *= interestRate;
                ++totalYears;
            }
            Console.WriteLine("In {0} year {1} you‘ll have the blance of {2}.", totalYears, totalYears == 1 ? "" : "s", balance);//三元
            if (totalYears == 0)
                Console.WriteLine("To be honest,you realy didn‘t need to use this calculator.");
            Console.ReadKey();
        }
    }
}
  • for循环:(利用for循环显示一个Mandelbrot曼德布洛特集合)
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //(Mandelbrot)曼德布洛特集合
            double realCoord, imagCoord;
            double realTemp, imagTemp, realTemp2, arg;
            int iterations;
            for (imagCoord = 1.2; imagCoord >= -1.2; imagCoord -= 0.05)
            {
                for (realCoord = -0.6; realCoord <= 1.77; realCoord += 0.03) 
                {
                    iterations = 0;
                    realTemp = realCoord;
                    imagTemp = imagCoord;
                    arg = (realCoord * realCoord) + (imagCoord * imagCoord);
                    while ((arg < 4) && (iterations < 40))
                    {
                        realTemp2 = (realTemp * realTemp) - (imagTemp * imagTemp) - realCoord;
                        imagTemp = (2 * realTemp * imagTemp) - imagCoord;
                        realTemp = realTemp2;
                        arg = (realTemp * realTemp) + (imagTemp * imagTemp);
                        iterations += 1;      
                    }
                    switch (iterations % 4)
                    { 
                case 0:
                    Console.Write(".");
                    break;
                case 1:
                    Console.Write("o");
                    break;
                case 2:
                    Console.Write("O");
                    break;
                case 3:
                    Console.Write("@");
                    break;
                    }
                }
            Console.Write("\n");
            } 
            Console.ReadKey();        
        }
    }
}

Mandelbrot集合中的每个位置都对应于公式N=x+y*i中的一个复数。实数部分是x,虚数部分是y,i是-1的平方根。图像中的各个位置的x和y坐标对应于复数的x和y部分。图像中的每个位置用参数N来表示,它是x*x+y*y的平方根。如果这个值大于或者等于2,则这个数字对应的位置是0。如果这个参数小于2,就把N的值改为N*N-N(即N=(x*x-y*y-x)+(2*x*y-y)*i),并再次测试这个新的N值。如果这个值大于或者等于2,则这个值对应位置是1.这个过程将一直继续下去,直到给图像中的位置赋一个值,或迭代执行的次数超过指定的次数为止。根据给图像中每个点赋予的值,在图形环境下,屏幕上会显示某种颜色的像素。但是本例使用的文本环境,所以屏幕上显示的是一个字符。

  • 循环的中断
    1. break——立即终止循环
    2. continu——立即终止当前循环(继续执行下一次循环)
    3. goto——可以跳出循环,到已标记好的位置上(代码不易阅读和理解)
    4. return——跳出循环及其包含的函数
  • 无限循环(略)
  • 三元运算符,语法如下:

               <test>?<resultIfTrue>:<resultIfFalse>

     其中计算<test>可以得到一个bool值,运算结果根据这个bool值来确定是<resultIfTrue>还是<resultIfFalse>;

     举例:

string resultString=(myInterger<10) ? "Less than 10":"Greater than or equal to 10";

 

把那个字符串赋给resultString,取决于myInterger的值与10的比较。如果myInterger的值小于10,就把第一个字符串赋给resultString;如果myInterger的值大于或者等于10,就把第二个字符串赋给resultString。这个运算符比较适用于这样简单赋值语句,但不适用于根据比较结果执行大量代码的情形。此时应该使用if语句。

C#编程(3_流程控制)

标签:style   blog   color   io   ar   使用   for   sp   div   

原文地址:http://www.cnblogs.com/haizhibin1989/p/4051345.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!