zoukankan      html  css  js  c++  java
  • 2.4 逻辑运算符

    使用逻辑运算符

    运算符 描述 示例
    逻辑非 !expression
    && 逻辑与 expression1&&expression2
    || 逻辑或 expression1||expression2
     1 // Designers Network
     2 // Demonstrates logical operators
     3 
     4 #include <iostream>
     5 #include <string>
     6 using namespace std;
     7 
     8 int main() 
     9 {
    10     string username;
    11     string password;
    12     bool success;
    13 
    14     cout << "	Game Designer's Network
    ";
    15 
    16     do
    17     {
    18         cout << "
    Username: ";
    19         cin >> username; 
    20 
    21         cout << "Password: ";
    22         cin >> password; 
    23 
    24         if (username == "S.Meier" && password == "civilization")
    25         {
    26             cout << "
    Hey, Sid.";
    27             success = true;
    28         }
    29 
    30         else if (username == "S.Miyamoto" && password == "mariobros")
    31         {
    32             cout << "
    What's up, Shigeru?";
    33             success = true;
    34         }
    35 
    36         else if (username == "W.Wright" && password == "thesims")
    37         {
    38             cout << "
    How goes it, Will?";
    39             success = true;
    40         }
    41 
    42         else if (username == "guest" || password == "guest")
    43         {
    44             cout << "
    Welcome, guest.";
    45             success = true;
    46         }
    47 
    48         else
    49         {
    50             cout << "
    Your login failed.";
    51             success = false;
    52         }
    53     } while (!success);
    54 
    55     return 0;
    56 }

    每个成员必须输入用户名和密码来登录,如果登录成功,成员将收到对其个人的问候。若要作为游客访问等罗,用户只需要在用户名或密码提示的后面输入guest即可。

     不是成员或访客则无法登录!

     访客guest登录!

     看来今天有精英登陆了!!!

    运算符的优先级

    逻辑非!比逻辑与&&优先级高。逻辑与&&比逻辑或||优先级高。

    如果希望对低优先级的运算符进行计算,可以使用括号。

    使用冗余括号是一门艺术。

  • 相关阅读:
    mysql 安装教程
    Centos 7和 Centos 6开放查看端口 防火墙关闭打开
    yum源中默认好像是没有mysql的。为了解决这个问题,我们要先下载mysql的repo源。
    CentOS更改yum源与更新系统
    Linux中文显示乱码?如何设置centos显示中文
    centos 7 升级/安装 git 2.7.3
    Maven实现项目构建直接部署Web项目到Tomcat
    ODAC (odp.net) 从开发到部署
    OGNL的使用
    DotNet Core 中使用 gRPC
  • 原文地址:https://www.cnblogs.com/wlyperfect/p/12400493.html
Copyright © 2011-2022 走看看