码迷,mamicode.com
首页 > 其他好文 > 详细

if条件语句 for循环语句

时间:2014-08-19 09:19:14      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:for   ar   问题   amp   line   new   ad   ef   

 (一)if语句

      If语句是一个条件分支语句。

      C#中,空语句是合法的,他不做任何操作。

     好的编码风格要求语句块中要有缩进。

I f语句中if后面可以没有else,可以有多个else if,但只能最多有一个else!

 

例题:

/*if语句的简单应用

 收入和逃税问题

 

            double money;

            Console.Write("月收入");

            money = Convert.ToDouble(Console.ReadLine());

            if (money > 3500)

            {

                Console.WriteLine("交税");

            }

            else

            {

                Console.WriteLine("逃税");

            }

            Console.WriteLine("恭喜");

            学习断点( 工具 选项 编辑 文本行号 调试)

 

 

 

/*男女身高体重健康值信息查看

            Console.Write("性别:");

            string sex = Console.ReadLine();

            Console.Write("身高:");

            int h =Convert.ToInt32 ( Console.ReadLine());

            Console.Write("体重:");

            int w = Convert.ToInt32(Console.ReadLine());

 

            int m = 0, n = 0;

            if(sex=="男")

            {

             m=h-100;

                if(m-w>=-3&&m-w<=3)

                {

                    Console.WriteLine ("标重");

 

                }

                else if(m-w<-3)

                {

                    Console .WriteLine ("您胖了");

                }

                else

                {

                    Console .WriteLine ("您瘦了");

                }

            }

        

            else if(sex=="女")

            {

                n=h-110;

                 if(n-w>=-3&&n-w<=3)

                {

                    Console.WriteLine ("标重");

 

                }

                else if(n-w<-3)

                {

                    Console .WriteLine ("您胖了");

                }

                else

                {

                    Console .WriteLine ("您瘦了");

                }

 

            }

            else

            {

 

                Console .WriteLine ("你输入错误");

            }

 

 

 

*/

 

            */

求一元二次方程根的情况

/*    Console.WriteLine("求一元二次方程a*x*x+b*x+c=0根的情况");

            Console.Write("a=");

            int a=Convert .ToInt32 (Console .ReadLine());

            Console.Write("b=");

            int b= Convert.ToInt32(Console.ReadLine());

            Console.Write("c=");

            int c = Convert.ToInt32(Console.ReadLine());

 

 

            if (a == 0)

            {

                Console.WriteLine("不是一元二次方程");

            }

            else

            {

                Console.WriteLine("一元二次方程");

                int delta = b * b - 4 * a * c;

            if(delta>0)

            {

                Console.WriteLine("两个不等的实数根");

            }

            else if (delta == 0)

            {

                Console.WriteLine("两个相等的实数根");

            }

            else

            {

                Console.WriteLine("无实数根");

            }

           

           

            }*/

  房子 钱 能力 的比较

 

 

 

 

           /*

      错误源分析:read改为readline

 

           Console.WriteLine("请输入三样东西");

           Console.WriteLine("你有房吗?");

           bool house = Convert.ToBoolean(Console.Read());

           Console.WriteLine("你有钱吗?");

           bool money = Convert.ToBoolean(Console.Read());

           Console.WriteLine("你有能力吗?");

           bool ability = Convert.ToBoolean(Console.Read());

 

           if (house == true)

           {

               Console.WriteLine("马上结婚");

           }

           else if (money == true)

           {

              Console.WriteLine("先买房再结婚");

           }

           else if (ability == true)

           {

               Console.WriteLine("先挣钱在买房在结婚");

           }

           else

              Console.WriteLine("拜拜");

            */

 

 

 

 

            /*语句错误源分析  因未加中阔号引起

            注意else后边加中括号

 

           Console.WriteLine("请输入三样东西");

           Console.Write ("你有房子吗?");

           

           bool house = Console.ReadLine() == "有";

           if (house == true)

           {

               Console.WriteLine("马上结婚");

           }

           else

           

            Console.Write("你有钱吗?");

           bool money=Console.ReadLine()=="有";

           if (money == true)

           {

               Console.WriteLine("先买房在结婚");

           }

           else

               Console.Write("你有能力吗?");

           bool ability = Console.ReadLine() == "有";

           if (ability == true)

           {

               Console.WriteLine("先挣钱在买房在结婚");

           }

           else

               Console.WriteLine("拜拜");

 

           */

 

 

          

          

 

 

/*

房 钱 能力 正确版本 此版本全部回答后作出分析

 

            Console.WriteLine("请输入三样东西");

            Console.WriteLine("你有房吗?");

            bool house = Convert.ToBoolean(Console.ReadLine());

            Console.WriteLine("你有钱吗?");

            bool money = Convert.ToBoolean(Console.ReadLine());

            Console.WriteLine("你有能力吗?");

            bool ability = Convert.ToBoolean(Console.ReadLine());

            if (house == true)

            {

                Console.WriteLine("马上结婚");

            }

            else if (money == true)

            {

                Console.WriteLine("先买房再结婚");

            }

            else if (ability == true)

            {

                Console.WriteLine("先挣钱在买房在结婚");

            }

            else

            {

                Console.WriteLine("拜拜");

            }

 

*/

 

/*

 此版本更新创意

            Console.WriteLine("请输入三样东西");

            Console.WriteLine("你有房吗?");

            bool house = Console.ReadLine()=="有";

            Console.WriteLine("你有钱吗?");

            bool money = Console.ReadLine()=="有";

            Console.WriteLine("你有能力吗?");

            bool ability =Console.ReadLine()=="有";

 

            if (house == true)

            {

                Console.WriteLine("马上结婚");

            }

            else if (money == true)

            {

                Console.WriteLine("先买房再结婚");

            }

            else if (ability == true)

            {

                Console.WriteLine("先挣钱在买房在结婚");

            }

            else

            {

                Console.WriteLine("拜拜");

            }

 

        */

 

          

 

 

/*此语句不需要全部回答问题,找到有效条件即可停止

 

           Console.WriteLine("请输入三样东西");

            Console.Write("你有房子吗?");

 

            bool house = Console.ReadLine() == "有";

                if (house == true)

                {

                    Console.WriteLine("马上结婚");

                }

                else

 

                    { 

                                Console.Write("你有钱吗?");

                                bool money = Console.ReadLine() == "有";

                                if (money == true)

                                {

                                    Console.WriteLine("先买房在结婚");

                                }

 

                                else

                                {

                                    Console.Write("你有能力吗?");

                                    bool ability = Console.ReadLine() == "有";

                                    if (ability == true)

                                    {

                                        Console.WriteLine("先挣钱在买房在结婚");

                                    }

                                    else

                                    { Console.WriteLine("拜拜"); }

                                }

                    }

            */

 

 

单机猜拳游戏

 

  /*    Console.WriteLine("单机猜拳游戏");

 

 

 

            int 剪刀 = 0, 石头 = 1, 布 = 2;

          int m=剪刀,n=石头,q=布;

            Random b = new Random();

            int bb = b.Next(3);

            int aa = Convert.ToInt32(Console .ReadLine ());

 

            if (aa == 0&&bb==0)

            {

                Console.WriteLine("人:"+m);

                Console.WriteLine("电脑:"+m);

                Console .WriteLine("平");

            }

            else if (aa == 0 && bb == 1)

            {

                Console.WriteLine("人:" + m);

                Console.WriteLine("电脑:" +n);

                Console.WriteLine("输");

            }

            else if (aa == 0 && bb == 2)

            {

                Console.WriteLine("人:" + m);

                Console.WriteLine("电脑:" + q);

                Console.WriteLine("赢");

            }

            else if (aa == 1 && bb == 0)

            {

                Console.WriteLine("人:" + n);

                Console.WriteLine("电脑:" + m);

                Console.WriteLine("赢");

            }

            else if (aa == 1 && bb == 1)

            {

                Console.WriteLine("人:" + n);

                Console.WriteLine("电脑:" +n);

                Console.WriteLine("平");

            }

            else if (aa == 1 && bb == 2)

            {

                Console.WriteLine("人:" +n);

                Console.WriteLine("电脑:" + q);

                Console.WriteLine("输");

            } else if (aa == 2 && bb == 0)

            {

                Console.WriteLine("人:" +q);

                Console.WriteLine("电脑:" + m);

                Console.WriteLine("输");

            }

            else if (aa == 2 && bb == 1)

            {

                Console.WriteLine("人:" + q);

                Console.WriteLine("电脑:" + n);

                Console.WriteLine("赢");

            }

            else if (aa == 2 && bb == 2)

            {

                Console.WriteLine("人:" + q);

                Console.WriteLine("电脑:" + q);

                Console.WriteLine("平");

            }

            else {

                Console.WriteLine("无意义");

            }

       

       

        */

 

 

 

//for循环语句应用

            //穷举法包括全部显示和用条件筛选出合适的值,迭代法求和的

            //(1初始条件;2循环条件;4状态改变)

           // {

             //   3循环体

            //}

           

           // for (int i = 0; i < 128; i++)

           //{

               // Console.WriteLine(i);

              //  Console.Write(i+"\t");

              // Console.Write((char)i + "\t");

              //Console.Write(i+"="+(char)i + "\t");

               /*

                if(i>2)

                {

                    Console.Write(i+"="+(char)i + "\t");

                }

                */

 

 

 

           // }

 

           /* for (int i = 1; i <= 100; i++)

            {

               // if(i%2==1)一百内的所有奇数

               // if(i%7==0||i%10==7||i/10==7)

                {

 

                    Console .Write (i);

                }

            }*/

 

 

            /*

            int sum=0;

            for (int i = 1; i <= 100; i++)

            {

                sum = sum + i;

            }

            Console.WriteLine(sum);求一到一百的和

            */

 

 

           /*

            int sum = 0;

            for (int i = 1; i <= 100; i++)

            {

                sum = sum + i;

                Console.WriteLine(sum);显示一到一百求和的每个过程值

            }

            */

 

 

 

          /*

            int n = 1;

            for (int i = 1; i <= 100; i++)

            {

                n = n + 2;

            }

            Console.WriteLine(n);

           */

 

 

 

 

            //一个猴子,一堆桃,猴子每天吃总数的一半,然后从剩下的扔一个坏的,到第七天早上只有一个桃子,求总数

            /*

            int n=1;

            for (int i = 1; i <= 6; i++)

            {

                n=2*(n+1);

            }

            Console.WriteLine(n);

            */

 

 

            //一对兔子开始为幼兔,第二个月为未成年,第三个月为成年兔,成年兔从第三个月开始每月生育一对兔子。

            //问开始有一对,24月有多少对?解析:第n+2个月增加的对数等于第n个月的对数!所以每个月对数等于前两月之和

            /*

            int tu1 = 1, tu2 = 1, tu=0;

            for (int i = 3; i <= 24; i++)

            {

                tu = tu1 + tu2;

                tu2 = tu1;

                tu1 = tu;

                 }

            Console.WriteLine(tu);

        

           */

 

 

           // 长方形

            /*

 

            for(int i=1;i<=5;i++)

            {

                for (int n = 1; n <= 10; n++)

            {

                Console.Write( "★");

                

            }

                Console.Write("\n");

              

 

            }

 

            */

 

 

            /*

            ★

            ★★

            ★★★

           

            for (int i = 1; i <= 5; i++)

            {

                for (int n = 1; n <= i; n++)

                {

                    Console.Write("★");

 

                }

                Console.Write("\n");

           

            }

            */

 

/*

★★★

★★

          

            for (int i = 1; i <= 5; i++)

            {

                for (int n = 1; n <= 6-i; n++)

                {

                    Console.Write("★");

 

                }

                Console.Write("\n");

 

            }

 

*/

            

/*

★★★

★★

 

         

            for (int i = 1; i <= 5; i++)

            {

                for (int n = 5; n >= i; n--)

                {

                    Console.Write("★");

 

                }

                Console.Write("\n");

 

            }

        

*/

 

 

           // 小九九算数

            /*

             for (int i = 1; i <= 9; i++)

            {

                for (int n = 1; n <= i; n++)

                {

                    Console.Write(i+"*"+n+"="+i*n);

 

                }

                Console.Write("\n");

 

            }

 

            */

 

 

 

 

/*

    ★注意:一个五角星占两个空格

  ★★

★★★

           

 

            for (int i = 1; i <= 5; i++)

            {

                for (int n = 1; n <= 5 - i; n++)

                {

                    Console.Write("  ");

 

                }

                for (int m = 1; m <= i;m++ )

                {

                    Console.Write("★");

                }

                Console.Write("\n");

 

            }

 

           */

 

 

/*

★★★

  ★★

    ★

        

            for (int i = 1; i <= 5; i++)

            {

                for (int n = 1; n < i; n++)

                {

                    Console.Write("  ");

 

                }

                for (int m = 1; m <= 6-i; m++)

                {

                    Console.Write("★");

                }

                Console.Write("\n");

 

            }

           

            */

 

 

 

 

/*

    ★

  ★★★

★★★★★          

           

            for (int i = 1; i <= 5; i++)

            {

                for (int n = 1; n <=6- i; n++)

                {

                    Console.Write("  ");

 

                }

                for (int m = 1; m <i*2; m++)

                {

                    Console.Write("★");

                }

                Console.Write("\n");

 

            }

 

            */

 

/*

    ★

  ★★★

★★★★★

  ★★★

    ★

 

            for (int i = 1; i <= 9; i++)

            {

 

                if (i <= 5)

                {

 

                    for (int n = 1; n <= 5 - i && i <= 5; n++)

                    {

                        Console.Write("  ");

 

                    }

                    for (int m = 1; m < i * 2 && i <= 5; m++)

                    {

                        Console.Write("★");

 

 

                    }

 

 

 

                   

 

                }

                else

                {

 

                    for (int q = 6; q <= i; q++)

                    {

 

                        Console.Write("  ");

                    }

                    for (int w = 6; w < 25 - 2 * i ; w++)

                    {

                        Console.Write("★");

                    }

 

 

 

                }

                Console.Write("\n");

 

            }

            */

 

 

 

 

 

if条件语句 for循环语句,布布扣,bubuko.com

if条件语句 for循环语句

标签:for   ar   问题   amp   line   new   ad   ef   

原文地址:http://www.cnblogs.com/wanwuguizong/p/3921187.html

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