C#流程控制————选择结构

1.选择结构——if else if

if else if

作用:用来处理多条件的区间性的判断

语法:

if(判断条件)

{

         要执行的代码;

}else if(判断条件)

{

         要执行的代码;

}

else if(判断条件)

{

         要执行的代码;

}else

{

          要执行的代码;

}

执行过程:程序首先判断第一个if所带的小括号中的判断条件,如果条件成立,也就是返回一个true,
则执行该if所带的大括号中的代码,执行完成后,立即跳出if else-if结构。
如果第一个if所带的判断条件不成立,也就是返回一个false,则继续向下进行判断,依次的判断每一个if else的判断条件,如果成立,就执行该if所带的大括号中的代码,如果不成立,则继续向下判断,
如果每个if所带的判断条件都不成立,就看当前这个if else-if结构中是否存在else。
如果有else的话,则执行else中所带的代码,如果没有else,则整个if-else if神马都不做。

else可以省略。

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

namespace _02_My_Second_Demo
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //对学员的结业考试成绩评测
            //成绩 >= 90:A
            //90>成绩>=80:B
            //80>成绩>=70:C
            //70>成绩>=60:D
            //成绩 < 60:E
            Console.WriteLine("请输入学员的考试成绩");
            int score = Convert.ToInt32(Console.ReadLine());

            if (score >= 90)
            {
                Console.WriteLine("A");
            }
            else if (score >= 80)
            {
                Console.WriteLine("B");
            }
            else if (score >= 70)
            {
                Console.WriteLine("C");
            }
            else if (score >= 60)
            {
                Console.WriteLine("D");
            }
            else
            {
                Console.WriteLine("E");
            }
            Console.ReadKey();
        }
    }
}


2.练习题

1.比较三个数的大小,不考虑相等。

2.练习1:提示用户输入密码,如果密码是“88888”则提示正确,否则要求再输入一次,
   如果密码是“88888”则提示正确,否则提示错误,程序结束。
(如果我的密码里有英文还要转换吗,密码:abc1)

3.练习2:提示用户输入用户名,然后再提示输入密码,如果用户名是“admin”并且密码是“88888”,
 则提示正确,否则,如果用户名不是admin还提示用户用户名不存在,
 如果用户名是admin则提示密码错误.

4.练习3:提示用户输入年龄如果大于等于18,则告知用户可以查看,如果小于10岁,
   则告知不允许查看,如果人于等于10岁并且小于18,
   则提示用户是否继续查看(yes、no),如果输入的是yes则提示用户请查看,
   否则提示”退出,你放弃查看”。

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

namespace _02_My_Second_Demo
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //比较3个数字的大小不考虑相等
            //分别的提示用户输入三个数字我们接受并且转换成int类型
            Console.WriteLine("请输入第一个数字");
            int numberOne = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("请输入第二个数字");
            int numberTwo = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("请输入第三个数字");
            int numberThree = Convert.ToInt32(Console.ReadLine());

            //三种情况 应该使用if else-if来做
            //如果第一个数字大于第二个数字并且第一个数字还大于第三个数字
            if (numberOne > numberTwo && numberOne > numberThree)
            {
                Console.WriteLine(numberOne);
            }
            //如果第二个数字大于第一个数字并且第二个数字大于第三个数字
            else if (numberTwo > numberOne && numberTwo > numberThree)
            {
                Console.WriteLine(numberTwo);
            }
            //如果第三个数字大于第一个数字并且第三个数字大于第二个数字
            else
            {
                Console.WriteLine(numberThree);
            }
            Console.ReadKey();

            //练习1:提示用户输入密码,如果密码是“88888”则提示正确,否则要求再输入一次,
            //如果密码是“88888”则提示正确,否则提示错误,程序结束。
            //(如果我的密码里有英文还要转换吗,密码:abc1)
            Console.WriteLine("请输入密码");
            string pwd = Console.ReadLine();
            if (pwd == "888888")
            {
                Console.WriteLine("登陆成功");
            }
            else//要求用户再输入一次
            {
                Console.WriteLine("密错误,请重新输入");
                pwd = Console.ReadLine();
                if (pwd == "888888")
                {
                    Console.WriteLine("输了两遍,终于正确了");

                }
                else//输入第二次错误
                {
                    Console.WriteLine("两边都不对,程序结束");
                }
            }
            Console.ReadKey();

            //练习2:提示用户输入用户名,然后再提示输入密码,如果用户名是“admin”并且密码是“88888”,
            //则提示正确,否则,如果用户名不是admin还提示用户用户名不存在,
            //如果用户名是admin则提示密码错误.
            Console.WriteLine("请输入用户名");
            string name = Console.ReadLine();
            Console.WriteLine("请输入密码");
            string pwd1 = Console.ReadLine();
            //第一种情况:用户名和密码全部输入正确
            if (name == "admin" && pwd1 == "888888")
            {
                Console.WriteLine("登陆成功");
            }
            //第二种情况:密码错误
            else if (name == "admin")
            {
                Console.WriteLine("密码输入错误,程序退出");
            }
            //第三种情况:用户名错误
            else if (pwd1 == "888888")
            {
                Console.WriteLine("用户名错误,程序退出");
            }
            //第四种情况:用户名和密码都错
            else
            {
                Console.WriteLine("用户名和密码全部错误,程序退出");
            }
            Console.ReadKey();

            //练习3:提示用户输入年龄如果大于等于18,则告知用户可以查看,如果小于10岁,
            //则告知不允许查看,如果人于等于10岁并且小于18,
            //则提示用户是否继续查看(yes、no),如果输入的是yes则提示用户请查看,
            //否则提示”退出,你放弃查看”。
            // 第一种情况 >= 18
            //第二种情况 < 10
            //第三种情况>=10&&<18
            Console.WriteLine("请输入你的年龄");
            int age = Convert.ToInt32(Console.ReadLine());
            if (age >= 18)
            {
                Console.WriteLine("请查看!!!");
            }
            else if (age < 10)
            {
                Console.WriteLine("不允许查看");
            }
            else
            {
                Console.WriteLine("确定要看么?yes/no");
                //input 要么是yes要么是no
                string input = Console.ReadLine();
                if (input == "yes")
                {
                    Console.WriteLine("请查看!!!");
                }
                else
                {
                    Console.WriteLine("不允许查看");
                }
            }
            Console.ReadKey();

        }
    }
}


3.选择结构——switch case

switch-case:用来处理多条件的定值的判断。
语法:
switch(变量或者表达式的值)
{

      case值1:要执行的代码;
      break;
      case值2:要执行的代码;
      break;
      case值3:要执行的代码;
      break;
      default:要执行的代码;
      break;

}
执行过程:程序执行到switch处,首先将括号中变量或者表达式的值计算出来,
然后拿着这个值依次跟每个case后面所带的值进行匹配,一旦匹配成功,则执行
该case所带的代码,执行完成后,遇到break。跳出switch-case结构。
如果,跟每个case所带的值都不匹配。就看当前这个switch-case结构中是否存在
default,如果有default,则执行default中的语句,如果没有default,则该switch-case结构
什么都不做。

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

namespace _02_My_Second_Demo
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //李四的年终工作评定,如果定为A级,则工资涨500元,如果定为B级,
            //则工资涨200元,如果定为C级,工资不变,如果定为D级工资降200元,
            //如果定为E级工资降500元.
            //设李四的原工资为5000,请用户输入李四的评级,然后显示李四来年的工资

            bool b = true;
            double salary = 5000;
            Console.WriteLine("请输入对李四的年终评定"); //a b c d e乱七八糟
            string level = Console.ReadLine();
            switch (level)
            {
                case "A":
                    salary += 500;
                    break;
                case "B":
                    salary += 200;
                    break;
                case "C":
                    break;
                case "D":
                    salary -= 200;
                    break;
                case "E":
                    salary -= 500;
                    break;
                default:
                    Console.WriteLine("输入有误,程序退出");
                    b = false;
                    break;
            }
            if (b)
            {
                Console.WriteLine("李四明年的工资是{0}元", salary);
            }
            Console.ReadKey();

        }
    }
}


4.练习题

让用户输入姓名,然后显示出这个人上辈子是什么职业。

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

namespace _02_My_Second_Demo
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //让用户输入姓名,然后显示出这个人上辈子是什么职业。

            Console.WriteLine("请输入一个姓名,我们将显示出来这个人上辈子的职业");
            string name = Console.ReadLine();
            //老杨、老苏、老马、老蒋、老牛、老虎、老赵
            switch (name)
            {
                case "老杨":
                    Console.WriteLine("医生");
                    break;
                case "老苏":
                    Console.WriteLine("警察");
                    break;
                case "老马":
                    Console.WriteLine("老师");
                    break;
                case "老蒋":
                    Console.WriteLine("程序员");
                    break;
                case "老牛":
                    Console.WriteLine("老板");
                    break;
                case "老虎":
                    Console.WriteLine("明星");
                    break;
                case "老赵":
                    Console.WriteLine("销售");
                    break;
                default:
                    Console.WriteLine("不认识");
                    break;
            }
            Console.ReadKey();

        }
    }
}


5.if else if 和switch case的区别

相同点:都可以实现多分支结构
不同点:
·if-else if:可以处理范围
·switch:一般只能用于等值比较
三者的区别:
if有条件的执行一条语句
if-else有条件的执行一条或另一条语句
switch有条件的执行一组语句中的一条语句

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

namespace _02_My_Second_Demo
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //成绩>=90:A
            //90>成绩>=80:B
            //80>成绩>=70:C
            //70>成绩>=60:D
            // 成绩 < 60:E
            Console.WriteLine("请输入一个考试成绩");
            int score = Convert.ToInt32(Console.ReadLine()); //0-100
            switch (score / 10)//你要把范围变成一个定值
            {
                case 10://case 10要执行的代码跟case 9是一样的
                case 9:
                    Console.WriteLine("A");
                    break;
                case 8:
                    Console.WriteLine("B");
                    break;
                case 7:
                    Console.WriteLine("C");
                    break;
                case 6:
                    Console.WriteLine("D");
                    break;
                default:
                    Console.WriteLine("E");
                    break;
            }
            Console.ReadKey();
        }
    }
}


6.练习题

请用户输年份,再输入月份,输出该月的天数.(结合何判断闰年来做)。

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

namespace _02_My_Second_Demo
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //请用户输年份,再输入月份,输出该月的天数.(结合之前如何判断闰年来做)
            Console.WriteLine("请输入一个年份");
            try
            {
                int year = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("请输入一个月份");
                try
                {
                    int month = Convert.ToInt32(Console.ReadLine());//1-12
                    if (month >= 1 && month <= 12)
                    {
                        int day = 0;//声明一个变量用来存储天数
                        switch (month)
                        {
                            case 1:
                            case 3:
                            case 5:
                            case 7:
                            case 8:
                            case 10:
                            case 12:
                                day = 31;
                                break;
                            case 2:
                                //由于2月有平年和闰年的不同所以首先要判断一下当年是不是闰年
                                if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
                                {
                                    day = 29;
                                }
                                else
                                {
                                    day = 28;
                                }
                                break;
                            //4 6 9 11
                            default:
                                day = 30;
                                break;
                        }
                        Console.WriteLine("{0}年{1}月有{2}天", year, month, day);
                    }
                    else
                    {
                        Console.WriteLine("月份输入不规范,程序退出");
                    }
                }
                catch
                {
                    Console.WriteLine("月份输入有误,程序退出");
                }
            }//try年份的括号
            catch
            {
                Console.WriteLine("年份输入有误,程序退出");
            }
            Console.ReadKey();
        }
    }
}

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值