【2017-02-21】分支语句if...else...、分支嵌套、变量的作用域

语句是指程序命令,都是按照顺序执行的。

语句又分为:

            顺序语句:从上到下按顺序执行,挨个执行一遍。

            分支语句:选择性执行语句,有的可能会执行,有的可能不执行。满足条件执行。

            循环语句:

一、分支语句 if...else...

(1)格式

if(条件)

{满足此条件要执行的代码}

else if(条件)

{满足此条件要执行的代码}

else    //否则

{不满足上述条件的要执行的代码}

(2)、

必须以if开头,可以是else if结束,也可以是else结束,也可以直接结束。
if (bool类型(比较表达式))
{
如果上面的条件成立,那么会执行这里面的代码
}
else if (bool类型 (比较表达式))
{
走这里的代码
}
else //只要上面条件都不成立,那么必走else里的代码
{

}

(3)练习题

1、“请输入年份:”
判断是否是闰年,“xxxx年是闰年”,“xxxx年不是闰年”

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 练习题1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("请输入年份:");
            int a = Convert.ToInt32(Console.ReadLine());
            if (a % 4 == 0 && a % 100 != 0)
            {
                Console.WriteLine(a+"年是闰年"); 
            }
            else if (a % 400 == 0)
            {
                Console.WriteLine(a + "年是闰年");
            }
            else
            {
                Console.WriteLine(a+"年不是闰年");
            }

            Console.ReadLine();
        }
    }
}

2、“请输入您的分数:”
小于0,大于100,“输入的分数有误!”
大于0,小于10,“不及格!学渣!”
小于60,“不及格!继续努力!”
大于等于60,“恭喜你!及格了!”
大于等于90,“学霸!很厉害!”

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 练习题2
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("请输入您的分数:");
            double a = Convert.ToDouble(Console.ReadLine());



            //小于0,大于100,“输入的分数有误!”
            //大于0,小于10,“不及格!学渣!”
            //小于60,“不及格!继续努力!”
            //大于等于60,“恭喜你!及格了!”
            //大于等于90,“学霸!很厉害!”


            if (a < 0 && a > 100)
            {
                Console.WriteLine("输入的分数有误!");
            }
            else if (a > 0 && a < 10)
            {
                Console.WriteLine("不及格!学渣!");
            }

            else if (a < 60)
            {
                Console.WriteLine("不及格,继续努力!");
            }
            else if (a >= 90)
            {
                Console.WriteLine("学霸!很厉害!");
            }


            else if (a >= 60)
            {
                Console.WriteLine("恭喜你,及格了!");

            }

            Console.ReadLine();
        }
    }
}

3、猜拳
“请输入您的手势(石头/剪子/布):”
“用户赢了” “电脑赢了” “平局”

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 练习题3
{
    class Program
    {
        static void Main(string[] args)
        {

            //让用户输入手势
            Console.Write("请输入您的手势:");
            string user = Console.ReadLine();

            //电脑生成手势
            //0石头 1剪刀 2布
            Random r =new Random();
            int com =r.Next(0,3);

            //手势对比
            int user1;
            if(user=="石头")
            {
                user1 = 0;
            }
            else if (user == "剪刀")
            {
                user1 = 1;
            }
            else
            {
                user1 = 2; 
            }

            //对比输赢

            
            if (user1 - com == -1 || user1 - com == 2)
            {
                Console.WriteLine("您赢了!");
            }
            else if (user1 - com == -2 || user1 - com == 1)
            {
                Console.WriteLine("电脑赢了!");
            }
            else
            {
                Console.WriteLine("平局!");            
            }

            Console.ReadLine();
        }
    }
}

4、人工智能对话
如果说的是同一句话,不一定要回复同一句

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 练习题4
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hi 我是Siri,请问您需要帮助吗?");
            string a=Console.ReadLine();

            Random r = new Random();
            int com = r.Next(0, 4);
            
            if (com==0)
            {
                Console.WriteLine("凡事靠自己,不要总想着靠别人");
               }  
               else if (com== 1) 
            {Console.WriteLine("需要帮助我也没啥好帮你的!");
            }
            else if (com == 2)
            {
                Console.WriteLine("不需要帮助你找我干嘛!!!");
            }
            else if (com == 3)
            {
                Console.WriteLine("乱写什么!问你需要还是不需要!!!");
            }

            Console.ReadLine();
        }
    }
}

二、分支嵌套、变量的作用域

1、分支嵌套就是在if或者else if 或者 else 下嵌套if...else...语句

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 分支嵌套
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 90;

            if (a < 0 || a > 100)//分数有误走这里
            {
                Console.WriteLine("分数输入有误!");
            }
            else //分数正确走这里
            {
                Console.WriteLine("分数输入正确!");
                if (a >= 60)
                {
                    Console.WriteLine("及格了!");

                    if (a >= 90)
                    {
                        Console.WriteLine("学霸!");
                    }
                }
                else
                {
                    Console.WriteLine("不及格!");

                    if (a < 10)
                    {
                        Console.WriteLine("学渣!");
                    }
                }

            }

        }
    }
}

2、变量的作用域

“儿子可以用爹的所有东西”   “爹不能用儿子的东西”

 

转载于:https://www.cnblogs.com/qq609113043/p/6430814.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值