zoukankan      html  css  js  c++  java
  • C# 分支语句

        在C#中,语句主要分为顺序语句,分支语句和循环语句。在本部分的学习中,主要讲解了分支语句。而分支语句主要包括if语句和switch语句。
        if语句主要有4种格式:
        1.if()
          {}
        2.if()
          {}
          else{}
        如果if满足,执行if,就不会再执行else;如果if不满足,就一定会执行else。(二者必选其一)
        3.if()
          {}
          else if()
          {}
          else
          {}
        只要上面有一个if或else if满足条件,执行了,从他以下的所有不需要再去判断读取。
        4.if的嵌套。

        例:依次输入年份、月份、日期,判断所输入的格式是否正确。

           

    Console.WriteLine("请输入一个年份:");
    int year = int.Parse(Console.ReadLine());
    if (year >= 0 && year <= 9999)
    {
    Console.WriteLine("请输入月份:");
    int month = int.Parse(Console.ReadLine());
    if (month >= 1 && month <= 12)
    {
    Console.WriteLine("请输入日期:");
    int date = int.Parse(Console.ReadLine());
    if (date >= 1 && date <= 31)
    {
    if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
    {
    Console.WriteLine("您所输入的时间为:" + year + "-" + month + "-" + date);
    }
    else if (month == 4 || month == 6 || month == 9 || month == 11)
    {
    if (date >= 1 && date <= 30)
    {
    Console.WriteLine("您所输入的时间为:" + year + "-" + month + "-" + date);
    }
    else
    {
    Console.WriteLine("您输入的日期有误!");
    }
    }
    else
    {
    if (date >= 1 && date <= 28)
    {
    Console.WriteLine("您所输入的时间为:" + year + "-" + month + "-" + date);
    }
    else if (date == 29)
    {
    if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
    {
    Console.WriteLine("您所输入的时间为:" + year + "-" + month + "-" + date);
    }
    else
    {
    Console.WriteLine("您输入的日期有误!");
    }
    }
    else
    {
    Console.WriteLine("您输入的日期有误!");
    }
    }
    }
    else
    {
    Console.WriteLine("您输入的日期有误!");
    }
    }
    else
    {
    Console.WriteLine("您输入的月份有误!");
    }
    }
    else
    {
    Console.WriteLine("您输入的年份有误!");
    }
    Console.ReadLine();

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

    Console.WriteLine("你有房子吗?");
    string s =Console.ReadLine();
    if (s == "有")
    {
    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语句与if语句中的第三种格式,if(){}else if(){}else{}相类似,都是表示多选一的情况,在一定条件下可以相互转换。

        switch语句的格式是:

    switch (s)//括号内是变量名称
    {
    case "1":
    break;
    case "2":
    break;
    case "3":
    break;
    case "4":
    break;
    default:
    break;
    }

        例:判断某个月的某一天,是一年的第几天?假设本年的2月份有28天。

        

    int m1 = 31, m2 = 28, m3 = 31, m4 = 30, m5 = 31, m6 = 30, m7 = 31, m8 = 31, m9 = 30, m10 = 31, m11 = 30;
    Console.WriteLine("请输入月份:");
    int m = int.Parse(Console.ReadLine());
    Console.WriteLine("请输入几号:");
    int d = int.Parse(Console.ReadLine());
    switch (m)
    {
    case 1:
    Console.WriteLine("第"+d+"天");
    break;
    case 2:
    Console.WriteLine("第" + (m1+d) + "天");
    break;
    case 3:
    Console.WriteLine("第" + (m1+m2+d) + "天");
    break;
    case 4:
    Console.WriteLine("第" +(m1+m2+m3+ d )+ "天");
    break;
    case 5:
    Console.WriteLine("第" +(m1+m2+m3+m4+ d) + "天");
    break;
    case 6:
    Console.WriteLine("第" + (m1+m2+m3+m4+m5+d) + "天");
    break;
    case 7:
    Console.WriteLine("第" +(m1+m2+m3+m4+m5+m6+ d) + "天");
    break;
    case 8:
    Console.WriteLine("第" + (m1+m2+m3+m4+m5+m6+m7+d) + "天");
    break;
    case 9:
    Console.WriteLine("第" + (m1+m2+m3+m4+m5+m6+m7+m8+d)+ "天");
    break;
    case 10:
    Console.WriteLine("第" + (m1+m2+m3+m4+m5+m6+m7+m8+m9+d) + "天");
    break;
    case 11:
    Console.WriteLine("第" + (m1+m2+m3+m4+m5+m6+m7+m8+m9+m10+d) + "天");
    break;
    case 12:
    Console.WriteLine("第" + (m1+m2+m3+m4+m5+m6+m7+m8+m9+m10+m11+d) + "天");
    break;
    default:
    Console.WriteLine("您输入的有误!");
    break;
    }

  • 相关阅读:
    wsdl和soap(简单使用TCPMon工具)03
    Django-7
    Django-6
    Django-5
    Django-4
    Django-3
    Django-2
    Django-1
    python_控制台输出带颜色的文字方法
    python网络编程
  • 原文地址:https://www.cnblogs.com/hongsen3/p/5697630.html
Copyright © 2011-2022 走看看