zoukankan      html  css  js  c++  java
  • 语句

    语句:三类:顺序、分支、循环

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

    (二)
    if(表达式)
    {
    }
    else
    {
    }

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

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

    练习题:

    //输入三个整数,xyz,最终以从小到大的方式输出。
    //利用嵌套。
    Console.Write("x=");
    int x = int.Parse(Console.ReadLine());
    Console.Write("y=");
    int y = int.Parse(Console.ReadLine());
    Console.Write("z=");
    int z = int.Parse(Console.ReadLine());

    if (x < y && x < z)
    {
       Console.WriteLine(x);
       if (y < z)
        {
          Console.WriteLine(y);
          Console.WriteLine(z);
        }
       else//z<y
       {
       Console.WriteLine(z);
       Console.WriteLine(y);
        }
    }
    else if (y < x && y < z)
    {
       Console.WriteLine(y);
       if (x < z)
       {
        Console.WriteLine(x);
        Console.WriteLine(z);
       }
      else
      {
      Console.WriteLine(z);
      Console.WriteLine(x);
      }
    }
    else//z是最小的
    {
      Console.WriteLine(z);
      if (x < y)
      {
       Console.WriteLine(x);
       Console.WriteLine(y);
       }
       else
       {
        Console.WriteLine(y);
        Console.WriteLine(x);
       }
    }

    Console.ReadLine();

    输入三个整数,xyz,最终以从小到大的方式输出。
    Console.WriteLine(x);
    Console.WriteLine(y);
    Console.WriteLine(z);
    利用中间变量
    Console.Write("x=");
    int x = int.Parse(Console.ReadLine());
    Console.Write("y=");
    int y = int.Parse(Console.ReadLine());
    Console.Write("z=");
    int z = int.Parse(Console.ReadLine());

    int zhong;
    if (x < y && x < z)
    {
       if (y < z)
       {

       }
       else//x<z<y
       {
        zhong = y;
        y = z;
        z = zhong;
        }
    }
    else if (y < x && y < z)
    {
        zhong = x;
        x = y;
        y = zhong;
       if (y < z)

       {
        }

       else
       {
        zhong = y;
         y = z;
         z = zhong;
        }
    }
    else
    {
       zhong = x;
        x = z;
       z = zhong;
       if (y < z)

      {
        }

    else//x<z<y
    {
    zhong = y;
    y = z;
    z = zhong;
    }
    }

    Console.WriteLine(x);
    Console.WriteLine(y);
    Console.WriteLine(z);


    Console.ReadLine();

    //switch case
    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;      //最后一个也需要跳出花括号
    }

  • 相关阅读:
    Python爬虫之Scrapy框架的简介和基础应用
    python爬虫之selenium,谷歌无头浏览器
    12.块设备驱动程序(磁盘)
    11.USB输入子系统
    10.触摸屏驱动
    9.LCD驱动架构
    8.platform驱动分离
    7.输入子系统框架
    5.标签管理
    4.分支管理
  • 原文地址:https://www.cnblogs.com/kevin2016/p/5276273.html
Copyright © 2011-2022 走看看