• C# 分支语句


    选择语句

    if,else

    if是如果的意思,else是另外的意思,if后面跟()括号内为判断条件,如果符合条件则进入if语句执行命令。如果不符合则不进入if语句。else后不用加条件,但是必须与if配合使用,else后也可加if,但if后需要条件。If-else可以嵌套。

    类似于条件运算符,其格式如下:

    if(表达式) //表达式返回值是True或False
    {
    }
    说明:
    1.表达式返回的是bool值;
    2.小括号和花括号后面不需要加分号。


    (二)
    if(表达式)
    {
    }
    else
    {
    }
    例:1.你能跑过豹子么?接收能或者不能。

    Console.Write("你能跑过豹子么?");
    string s = Console.ReadLine();
    if (s == "能")
    {
    Console.WriteLine("你比禽兽还禽兽!!");
    }
    else
    {
    Console.WriteLine("你连禽兽都不如!!");
    }
    Console.ReadLine();

    (三)
    if(表达式)
    {
    }
    else if
    {
    }
    else if
    {
    }
    ...
    else
    {
    }
    各种情况只能走其中之一,若上面的都没走,将执行else里面的。

    (四)
    if(表达式)
    {
        if(){}
        else{}
    }
    else
    {
       if(){}
    }
    if嵌套

    应用

    输入学生姓名,输入考试成绩 double
    若是100,【恭喜你***,满分通过!】
    若是大于等于80小于100,【**,你很优秀,继续保持!】
    若是大于等于60小于80,【**成绩良好】
    大于等于50小于60,【**就差一点点,下次一定要至少及格!】
    小于50,【**你是笨蛋么?】
    Console.Write("请输入您的姓名:");
    string name = Console.ReadLine();
    Console.Write("请输入您的分数:");
    double score = double.Parse(Console.ReadLine());
    if (score >= 0 && score <= 100)
    {
    if (score == 100)
    {
    Console.WriteLine(name+",恭喜你,满分通过!");
    }
    else if (score >= 80)
    {
    Console.WriteLine(name+",你很优秀!继续保持!");
    }
    else if (score >= 60)
    {
    Console.WriteLine(name+",成绩良好!");
    }
    else if (score >= 50)
    {
    Console.WriteLine(name + ",就差一点点!");
    }
    else
    {
    Console.WriteLine(name+",你是笨蛋么?");
    }
    }
    else
    {
    Console.WriteLine("输入有误!");
    }

    Console.ReadLine();

    分别输入年、月、日,判断日期格式是否正确!
    Console.Write("请输入年份:");
    int year = int.Parse(Console.ReadLine());
    if (year >= 0 && year <= 9999)
    {
    Console.Write("请输入月份:");
    int month = int.Parse(Console.ReadLine());
    if (month >= 1 && month <= 12)
    {
    Console.Write("请输入日:");
    int day = int.Parse(Console.ReadLine());
    if (day >= 1 && day <= 31)
    {
    if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
    {
    Console.WriteLine("日期格式正确,您输入的日期为:{0}-{1}-{2}。", year, month, day);
    }
    else if (month == 4 || month == 6 || month == 9 || month == 11)
    {
    if (day <= 30)
    {
    Console.WriteLine("日期格式正确,您输入的日期为:{0}-{1}-{2}。", year, month, day);
    }
    else//day==31
    {
    Console.WriteLine("日期格式错误!");
    }
    }
    else//2月
    {
    if (day <= 28)
    {
    Console.WriteLine("日期格式正确,您输入的日期为:{0}-{1}-{2}。", year, month, day);
    }
    else if (day == 29)
    {
    if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
    {
    Console.WriteLine("日期格式正确,您输入的日期为:{0}-{1}-{2}。", year, month, day);
    }
    else
    {
    Console.WriteLine("日期格式有误!!");
    }
    }
    else//day==30||31
    {
    Console.WriteLine("日期格式有误!");
    }
    }
    }
    else
    {
    Console.WriteLine("日输入有误!!");
    }
    }
    else
    {
    Console.WriteLine("月份输入有误!");
    }
    }
    else
    {
    Console.WriteLine("年份输入有误!!");
    }

    Console.ReadLine();

    相亲过程:你有房子么?你有钱么?你有能力么?
    【结婚吧】【先买房子在结婚】【先赚钱再买房子再结婚】都没有【拜拜~~】
    利用if嵌套做相亲过程

    Console.WriteLine("女:你有房子么?");
    string a = Console.ReadLine();
    if (a == "有")
    {
    Console.WriteLine("女:结婚吧");
    }
    else
    {
    Console.WriteLine("女:你有钱么?");
    string b = Console.ReadLine();
    if (b == "有")
    {
    Console.WriteLine("女:先买房子在结婚");
    }
    else
    {
    Console.WriteLine("女:你有能力么?");
    string c = Console.ReadLine();
    if (c == "有")
    {
    Console.WriteLine("女:先赚钱再买房子在结婚");
    }
    else
    {
    Console.WriteLine("女:拜拜~~");
    }

    }
    }
    Console.ReadLine();

    switch case

    switch case 必须与 break 一同使用。

    break是跳转语句。与switch case连用的时候是跳出最近的{}。

    switch case 与 if ,slse if ,slse if....slse(一样)

    Console.WriteLine("1.汉堡包");
    Console.WriteLine("2.薯条");
    Console.WriteLine("3.鸡块");
    Console.WriteLine("4.鸡腿");
    Console.WriteLine("5.鸡米花");

    Console.Write("请输入您的选择项目数字:");
    string a = Console.ReadLine();

    switch (a)//括号内是被判断的变量名称
    {
    case "1"://case后面的值是用来判断上面括号内的变量相不相等
    Console.WriteLine("您选择的是汉堡包");
    break;//break跳转语句,跳出最近的花括号
    case "2"://case与值之间有空格隔开 值后面是冒号
    Console.WriteLine("您选择的是薯条");
    break;
    case "3":
    Console.WriteLine("您选择的是鸡块");
    break;
    case "4":
    Console.WriteLine("您选择的是鸡腿");
    break;
    case "5":
    Console.WriteLine("您选择的是鸡米花");
    break; //最后一个也需要跳出花括号
    }

  • 相关阅读:
    【Java123】javapoet代码生成代码工具
    【Python123】OptionParser初使用
    【Spring123】JdbcTemplate初使用 以及 ORA-01858: a non-numeric character was found where a numeric was expected, ORA-00911: invalid character解决
    【Java123】解决javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated
    git常用命令
    在虚拟机上搭建自己的 git 服务器并创建 git 仓库
    git用法
    Golang gRPC框架3-自签证书验证
    go _nsq
    mysql的备份和恢复
  • 原文地址:https://www.cnblogs.com/1711643472qq/p/5695812.html
Copyright © 2020-2023  润新知