zoukankan      html  css  js  c++  java
  • 分支运算

    //语句分类:顺序语句,选择语句(分支语句),循环语句

    //选择分支语句

    //if(){}

    int a = 10;
    if (a < 13)
    {
    a++;
    }
    if (a > 3)
    {
    a--;
    }
    Console.WriteLine(a);
      

    if(){}else{} 二选一

    若if成立,则不去走else

    若if不成立,则一定会走esle

    int a = 10;
    if (a > 3)
    {
    a--;
    }
    else
    {
    a++;
    }
    Console.WriteLine(a); 

     

    if(){}else if(){}else if(){}else{} 多选一

    若if成立,那其他的所有都不去看

    若if不成立,去查看下一个else if 成立不成立 ,若成立剩下的全都不用看

    int a = 10;
    if (a >9)
    {
    a++;
    }
    else if (a > 3)
    {
    a++;
    }
    else 
    {
    a++;
    }
    Console.WriteLine(a);

    练习
    输入一个小于100的整数,判断:
    是小于10的
    两位数
    是100 

    Console.Write("输入一个小于100的整数:");
    int a = int.Parse(Console.ReadLine());
    if (a >= 0 && a <= 100)
    {
    
    if (a < 10)
    {
    Console.WriteLine("这是一个小于10的数");
    }
    else if (a < 100)
    {
    Console.WriteLine("这是一个双位数");
    }
    else
    {
    Console.WriteLine("这是数是100");
    }
    }
    
    else
    {
    Console.WriteLine("您的输入有误");
    } 

    输入三个整数,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
                    {
                        Console.WriteLine(z);
                        Console.WriteLine(y);
                    }
                }
                else if (z < y && z < x)
                {
                    Console.WriteLine(z);
                    if (x < y)
                    {
                        Console.WriteLine(x);
                        Console.WriteLine(y);
                    }
                    else
                    {
                        Console.WriteLine(y);
                        Console.WriteLine(x);
                    }
                }
                else
                {
                    Console.WriteLine(y);
                    if (x < z)
                    {
                        Console.WriteLine(x);
                        Console.WriteLine(z);
                    }
                    else
                    {
                        Console.WriteLine(z);
                        Console.WriteLine(x);
                    }
                } 

    两个数,将a的值给b,将b的值给a
    中间变量

    int a = 2;
    int b = 5;
    int z = a;
    a = b;
    b = z; 

    相亲过程
    女:你有房子么?
    若有→ 结婚吧;没有→ 你有钱么?
    若有→ 先买房子在结婚;
    没有→你有能力么?
    有→先赚钱再买房子在结婚
    没有→拜拜

    Console.WriteLine(" 相亲过程");
    Console.WriteLine("女:你有房子么?");
    string s = Console.ReadLine();
    if (s == "")
    {
    Console.WriteLine("结婚吧");
    }
    else if (s == "没有")
    {
    Console.WriteLine("你有钱么");
    s = Console.ReadLine();
    if (s == "")
    {
    Console.WriteLine("先买房在结婚吧");
    }
    else if (s == "没有")
    {
    Console.WriteLine("你有能力么?");
    s = Console.ReadLine();
    if (s == "")
    {
    Console.WriteLine("先赚钱再买房子在结婚吧");
    }
    else if (s == "没有")
    {
    Console.WriteLine("拜拜");
    }
    }
    }

     



  • 相关阅读:
    二维树状数组(模板)
    3033太鼓达人
    2503相框
    Ant Trip(画几笔)
    [ZJOI2004]嗅探器
    [USACO06JAN]冗余路径Redundant Paths(缩点)
    P3806 【模板】点分治1
    P4149 [IOI2011]Race
    P2634 [国家集训队]聪聪可可
    P4178 Tree
  • 原文地址:https://www.cnblogs.com/zbxiaoxu/p/5250910.html
Copyright © 2011-2022 走看看