zoukankan      html  css  js  c++  java
  • 关系、逻辑、条件运算符

    一、运算符:
    (一)、关系运算符: > < >= <= == !=
    1、bool a = 10 > 5; 关系比较的结果,类型一定是bool,
    必须用一个bool类型的变量来接收。
    (二)、逻辑运算符
    1、&& 并且:
    例如:10<11 && 10>9 :true, 10<11 && 10<9 :false

    PS:并且关系:只要有一个不成立,那么就返回false,
    如果想返回true,那么就必须全部都成立

    2、|| 或者:
    例如:10<11 || 10<9: true, 10<9 || 10>11 :false
    PS:或者关系:只要有一个成立,那么就返回true,
    如果想返回false,那么就必须全部都不成立

    3、! 非: 颠倒黑白

    (三)、条件运算符
    数据类型 a = 比较表达式 ? 值 : 值;
    例如:a = 10;
    b = 15;
    string a = ( a > b ) ? "对":"错";


    练习题
    1、
    请输入姓名:
    请输入性别(1/0):
    我叫xxx,我是xxx的。

                //接收用户输入的信息
                Console.Write("请输入姓名:");
                string name = Console.ReadLine();
                Console.Write("请输入性别(1/0):");
                int sex = Convert.ToInt32(Console.ReadLine());
    
                //计算比较 得出变量
                string x = sex == 1 ? "" : "";
    
                //打印输出
                Console.WriteLine("我叫" + name + ",我是" + x + "的。");
                
                Console.ReadLine();

    2、
    请输入姓名:
    请输入年龄:
    如果大于等于18岁,就输出: 我叫xxx,我成年啦!
    如果小于18岁,就输出: 我叫xxx,我还未成年。

                //接收用户输入的信息
                Console.Write("请输入姓名:");
                string name1 = Console.ReadLine();
                Console.Write("请输入年龄:");
                decimal age1 = Convert.ToDecimal(Console.ReadLine());
    
                //比较 得出变量
                string a = (age1 >= 18) ? "成年啦!" : "还未成年。";
    
                //输出打印
                Console.WriteLine("我叫" + name1 + ",我" + a);
    
                Console.ReadLine();

    3、
    请输入一个10 - 20 之间的整数:
    (此数字包含10和20)
    输出:
    你输入的数为:xxx,输入正确。
    或者
    你输入的数为:xxx,输入错误。

                //接收用户输入的信息
                Console.Write("请输入一个10-20间的整数(此数字包含10和20):");
                int a = Convert.ToInt32(Console.ReadLine());
    
                //比较 得出变量
                string a1 = (a >= 10 && a <= 20) ? ",输入正确。" : ",输入错误。";
    
                //输出打印
                Console.WriteLine("你输入的数为:" + a + a1);
    
                Console.ReadLine();

    4、请输入一个10-20之间的整数:
    (此数字包含10和20,但是不能等于15和18)
    输出:
    你输入的数为:xxx,输入正确。
    或者
    你输入的数为:xxx,输入错误。

                //接受用户输入的信息
                Console.Write("请输入一个10-20之间的整数:(此数字包含10和20,但是不能等于15和18):");
                int a = Convert.ToInt32(Console.ReadLine());
    
                //比较 得出变量
                string b = (a >= 10 && a <= 20 && a != 15 && a != 18) ? ",输入正确。" : ",输入错误。";
    
                //输出打印
                Console.WriteLine("你输入的数为:" + a + b);
                
                Console.ReadLine();

    5、
    请输入一个数0-20之间的整数:
    不能是0,可以是20。
    如果不为5,则最后增加“不为5。”;
    如果为5,则在“是10以内的数。”结束
    如果为15,则最后增加“不为15。”;
    如果为15,则在“是10-20之间的数。”结束

    注意,标点符号要正确!

    输出:
    你输入的数为:xxx,输入正确,是10以内的数,不为5。
    或者
    你输入的数为:xxx,输入正确,是10-20之间的数,不为15。
    或者
    你输入的数为:xxx,输入错误!

                //接收用户输入的信息
                Console.Write("请输入一个数0-20之间的整数:");
                int a = Convert.ToInt32(Console.ReadLine());
    
                //比较  得出变量
                string b = (a > 0 && a <= 20) ? ",输入正确" : ",输入错误";
                string c = (a > 0 && a <= 10) ? ",是10以内的数" : "";
                string d = (a > 10 && a <= 20) ? ",是10-20之间的数" : "";
                string e = (a > 0 && a <= 10 && a != 5) ? ",不为5" : "";
                string f = (a > 10 && a <= 20 && a != 15) ? ",不为15" : "";
    
                //输出 打印
                Console.WriteLine("你输入的数为:" + a + b + c + d + e + f + "");
    
                Console.ReadLine();

  • 相关阅读:
    remove white space from read
    optimize the access speed of django website
    dowload image from requests
    run jupyter from command
    crawl wechat page
    python version 2.7 required which was not found in the registry windows 7
    health
    alternate rows shading using conditional formatting
    word
    【JAVA基础】static 关键字
  • 原文地址:https://www.cnblogs.com/123lucy/p/5503520.html
Copyright © 2011-2022 走看看