zoukankan      html  css  js  c++  java
  • 15-07-02 语句-分支语句

    语句:

    语句的类型包括声明语句,表达式语句,选择语句,循环语句,跳转语句,异常语句。

    一:声明语句:入新的变量和常亮。变量声明可以选择为变量赋值。再常量声明中必须赋值。

    例:

    int i = 0;    //声明变量 i 并赋值,也可以不赋值。

    double a;

    二:表达式语句:用于计算值的表达式语句不许再变量中存储该值。

    例:

    sum = i + j;    //变量 i 和 j 在此之前必须先赋值,而且 sum 也需要声明类型。

    int x = a + b;   //或者在声明的同时进行运算。

    三:选择语句:if,else,switch,case

    四:循环语句:do,for,foreach,while

    五:跳转语句:break,continue,default,retrun

    六:异常语句:try-catch-finally

    顺序语句:从上到下依次执行

    分支语句

    1.if,else语句

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

    1. if (...)//括号内是判断条件
       {
       满足条件的时候执行;
       }

    2. if (...)//括号内是判断条件

      {
       满足条件的时候执行;
      }
      else
      {
      不满足条件时执行;
      }

    3. if (...)//括号内是判断条件
      {
      满足条件1的时候执行;
      }
      else if (...)
      {
      不满足条件1的情况下满足条件2;
      }

    4. if (...)//括号内是判断条件1
       {
          if (...)
         {
        既满足条件1又满足条件2的时候执行;
         }
       }

      else//最后不满足以上条件的执行这里的代码

      {

      }

      

    例:1.你有钱吗?你有房吗?你有车吗?你有能力吗?

           Console.WriteLine("你有钱吗?");
                string a = Console.ReadLine();
    
                if (a == "没有")
               {
                  Console.WriteLine("你有房吗?");
                  string b = Console.ReadLine();
                  if (b == "没有")
                  {
                    Console.WriteLine("你有车吗?");
                    string c = Console.ReadLine();
                    if (c == "没有")
                    {
                        Console.WriteLine("你有能力吗?");
                        string d = Console.ReadLine();
                        if (d == "没有")
                        {
                            Console.WriteLine("啥都没有,滚犊子吧!");
                        }
                        else if (d == "")
                        {
                            Console.WriteLine("那你要努力赚钱哦");
                        }
                        else
                        {
                            Console.WriteLine("你到底想说什么");
                        }
                    }
                    else if (c == "")
                    {
                        Console.WriteLine("有车也挺好,将来可以去旅行了");
                    }
                      else
                      {
                          Console.WriteLine("能不能好好说话");
                      }
                }
                else if (b == "")
                {
                    Console.WriteLine("有房也不错,我们谈谈吧!");
                }
                    else
                    {
                        Console.WriteLine("你说的什么?");
                    }
            }
                else if (a == "")
                {
                    Console.WriteLine("我们结婚吧!");
                }
                else
                {
                    Console.WriteLine("不想聊滚犊子!");
                }

    2.三个数中那个最大

           Console.WriteLine("请输入第一个数:");
                int a = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("请输入第二个数:");
                int b = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("请输入第三个数:");
                int c = Convert.ToInt32(Console.ReadLine());
    
                if (a >= b)
                {
                    if (a >= c)
                    {
                        Console.WriteLine("三个数中"+a+"最大");
                    }
                    else
                    {
                        Console.WriteLine("三个数中"+c+"最大");
                    }
                }
                else
                {
                    if (b >= c)
                    {
                        Console.WriteLine("三个数中"+b+"最大");
                    }
                    else
                    {
                        Console.WriteLine("三个数中"+c+"最大");
                    }
    
                }


    2.switch case语句

    switch case 必须与 break 一同使用。

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

    例:

                Console.WriteLine("查话费请按1,查余额请按2,查流量请按3,办理业务请按4,宽带请按5,集团业务请按7,人工服务请按0");
    
                int a = Convert.ToInt32(Console.ReadLine());
    
                switch (a)
                { 
                    case 1:
                        Console.WriteLine("您的本月话费为534元");
                        break;
                    case 2:
                        Console.WriteLine("您的余额不足");
                        break;
                    case 3:
                        Console.WriteLine("您的本月剩余流量为27M");
                        break;
                    case 4:
                        Console.WriteLine("没有什么业务好办的");
                        break;
                    case 5:
                        Console.WriteLine("宽带不稳定,不建议安装");
                        break;
                    case 6:
                        Console.WriteLine("你要打广告吗");
                        break;
                    case 0:
                        Console.WriteLine("我们下班了,明天再来吧");
                        break;
                    
                    default:
                        Console.WriteLine("欢迎致电中国坑爹热线,请查询后再播");
                    break;
                }
  • 相关阅读:
    【校招面试 之 C/C++】第23题 C++ STL(五)之Set
    Cannot create an instance of OLE DB provider “OraOLEDB.Oracle” for linked server "xxxxxxx".
    Redhat Linux安装JDK 1.7
    ORA-10635: Invalid segment or tablespace type
    Symantec Backup Exec 2012 Agent for Linux 卸载
    Symantec Backup Exec 2012 Agent For Linux安装
    You must use the Role Management Tool to install or configure Microsoft .NET Framework 3.5 SP1
    YourSQLDba介绍
    PL/SQL重新编译包无反应
    MS SQL 监控数据/日志文件增长
  • 原文地址:https://www.cnblogs.com/jia520110270/p/4623073.html
Copyright © 2011-2022 走看看