• if语句应用举例


    1、if(判断)      2、 if(判断)     3、 if(判断1)     4、if(判断1)

      {                    {                    {                     {

      (语句)              (语句1)           if(判断2)        (语句1)

      }                     }                      {                   }

                 else                    (语句1)       else if(判断2)

                     (语句2)          }                   {

                                   else                 (语句2)

                                              (语句2)               }

                             }                   ……

                               else                else

                                (语句3)          (语句3)

                             

    例:

    1、解方程a*x*x+b*x+c=0

                Console.WriteLine("求方程ax*x+bx+c=0的根的情况");
                Console.Write("a=");
                int a = Convert.ToInt32(Console.ReadLine());
                Console.Write("b=");
                int b = Convert.ToInt32(Console.ReadLine());
                Console.Write("c=");
                int c = Convert.ToInt32(Console.ReadLine());
    
                if (a == 0)
                {
                    Console.WriteLine("不是一元二次方程");
                }
                else
                {
                    Console.WriteLine("一元二次方程");
    
                    int derta = b * b - 4 * a * c;
                    if (derta > 0)
                    {
                        double x1 = (-b + Math.Sqrt(derta)) / (2 * a);
                        double x2 = (-b - Math.Sqrt(derta)) / (2 * a);
    
                        Console.WriteLine("有两个不等的实根:");
                        Console.WriteLine("x1=" + x1);
                        Console.WriteLine("x2=" + x2);
    
                    }
                    else if (derta == 0)
                    {
                        Console.WriteLine("有两个相等的实根:");
                        double m = -b / (2 * a);
                        Console.WriteLine("x=" + m);
                    }
                    else
                        Console.WriteLine("无实根");
                }

     

    2、判断体重是否标准

                Console.Write("性别:");
                String sex = Console.ReadLine();
                Console.Write("身高(cm):");
                int hight = Convert.ToInt32(Console.ReadLine());
                Console.Write("体重(kg):");
                int weight = Convert.ToInt32(Console.ReadLine());
    
                if (sex == "")
                {
                    int s = hight - 100;    //标准体重
                    if (weight > (s + 3))
                    {
                        Console.WriteLine("");
                    }
                    else if (weight <= (s + 3) && weight >= (s - 3))
                    {
                        Console.WriteLine("正常");
                    }
                    else
                        Console.WriteLine("");
                }
                else if (sex == "")
                {
                    int s = hight - 110;    //标准体重
                    if (weight > (s + 3))
                    {
                        Console.WriteLine("");
                    }
                    else if (weight <= (s + 3) && weight >= (s - 3))
                    {
                        Console.WriteLine("正常");
                    }
                    else
                        Console.WriteLine("");
                }
                else
                    Console.WriteLine("输入错误");

    3、跟电脑猜拳

                Console.Write("请出拳:");
                string human = Console.ReadLine();
                Console.WriteLine("人VS电脑:");
    
                Random a = new Random();  //定义一个随机数生成器
                int x = a.Next(3);  //随机生成一个3以内的整数
                String computer;    //定义一个字符串,为电脑出拳,下面为电脑的出拳
                if (x == 0)
                {
                    computer = "剪刀";
                }
                else if (x == 1)
                {
                    computer = "石头";
                }
                else
                    computer = "";
    
                Console.WriteLine(human + "VS" + computer);
    
                int b = 5;    //定义一个变量,用数字替代出拳,用来判断
                if (human != "剪刀" && human != "石头" && human != "")
                {
                    Console.WriteLine("输入错误");
                }
                else if (human == "剪刀")
                {
                    b = 0;
                }
                else if (human == "石头")
                {
                    b = 1;
                }
                else
                    b = 2;
    
                //判断出拳输赢
                if ((b - x == 1) || (x - b == 2))
                {
                    Console.WriteLine("你赢了");
                }
                else if ((x - b == 1) || (b - x == 2))
                {
                    Console.WriteLine("你输了");
                }
                else if (b == x)
                {
                    Console.WriteLine("平局");
                }
                else
                    Console.WriteLine("错误");
     
  • 相关阅读:
    python敏感词过滤
    wap移动端 设计图是px 自适应不同手机尺寸 px2rem vue样式初始化
    webpack学习笔记
    cordova混合App开发:Cordova+Vue实现Android APP开发 (app内打开浏览器及横竖屏) (七)
    cordova混合App开发:Cordova+Vue实现Android APP开发 (热更新) (六)
    cordova混合App开发:Cordova+Vue实现Android APP开发 (设置app图标和欢迎页) (五)
    cordova混合App开发:Cordova+Vue实现Android APP开发 (打包及调试) (四)
    cordova混合App开发:Cordova+Vue实现Android APP开发 (vue项目配置,打包到cordova) (三)
    cordova混合App开发:Cordova+Vue实现Android APP开发 (安装cordova框架生成app) (二)
    cordova混合App开发:Cordova+Vue实现Android APP开发 (环境搭建) (一)
  • 原文地址:https://www.cnblogs.com/phantom-k/p/3922227.html
Copyright © 2020-2023  润新知