zoukankan      html  css  js  c++  java
  • c++中的结构化语句 判断语句if 分支语句switch 循环语句 while 和 do while 循环语句for的使用

    作业1:

    使用if语句,根据1~7的数字,输出今天是星期几?的程序。

    方法一,直接使用单独的if语句

    #include <iostream>
    using namespace std;
    
    int main(){
        
        int numberWeek;
        cout<<"请输入一个1~7之间的整数"; 
        cin>>numberWeek;
        
    
        if(numberWeek==1){//写的判断条件不正确,例如:2<numberWeek<10 或10<numberWeek 判断的条件有== >= <= != > <  && || ! << >> & | ^  变量在前 x>10 2<x<10
            cout<<"今天是星期一"<<endl;
        }
        if(numberWeek==2){
            cout<<"今天是星期二"<<endl;
        }
        if(numberWeek==3){
            cout<<"今天是星期三"<<endl;
        }
        if(numberWeek==4){
            cout<<"今天是星期四"<<endl;
        }
        if(numberWeek==5){
            cout<<"今天是星期五"<<endl;
        }
        if(numberWeek==6){
            cout<<"今天是星期六"<<endl;
        }
        if(numberWeek==7){
            cout<<"今天是星期日"<<endl;
        }
    
        return 0;
    }

    方法二,使用关联的if语句

    #include <iostream>
    using namespace std;
    
    int main(){
        
        int numberWeek;
        cout<<"请输入一个1~7之间的整数"; 
        cin>>numberWeek;
        
        if(numberWeek==1){
            cout<<"今天是星期一"<<endl;
        }else if(numberWeek==2){
            cout<<"今天是星期二"<<endl;
        }else if(numberWeek==3){
            cout<<"今天是星期三"<<endl;
        }else if(numberWeek==4){
            cout<<"今天是星期四"<<endl;
        }else if(numberWeek==5){
            cout<<"今天是星期五"<<endl;
        }else if(numberWeek==6){
            cout<<"今天是星期六"<<endl;
        }else if(numberWeek==7){
            cout<<"今天是星期日"<<endl;
        }else{
            cout<<"您输入的数字不符合要求。"<<endl;
        }
        
    
        return 0;
    }

     练习题:完成1年12个月的季节输出,如果是8月输出这是夏天。

    作业2:

    使用switch语句,完成1年12个月的季节输出,如果是8月输出这是夏天,的程序。

    #include <iostream>
    using namespace std;
    
    int main(){
        
        int monthNumber;
        cout<<"请输入一个1~12之间的整数"; 
        cin>>monthNumber;
        
        switch (monthNumber){
            case 3:
            case 4:
            case 5:
                cout<<"这是春天"<<endl;
                break;
            case 6:
                cout<<"这是夏天"<<endl;
                break;
            case 7:
            case 8:
                cout<<"这是夏天"<<endl;
                break;
            case 9:
            case 10:
            case 11:
                cout<<"这是秋天"<<endl;
                break;
            case 12:
            case 1:
            case 2:
                cout<<"这是冬天"<<endl;
                break;
            default:
                 cout<<"您输入的数字不符合要求。"<<endl;
                 break;
        }
    
        
        return 0;
    }

    使用switch语句练习算术运算符的使用,任意输入两个浮点数,输出两个浮点数的 + - * /  %

    case :语句可以任意交换位置吗? default :语句可以写在任意位置吗?case 语句当中内容为空可以吗?都是可以的

    作业3:

    使用do while 和while 循环语句,完成输出一个*所组成的矩形,要求宽度为10 ,高度为20。

    while循环语句

    int x=1;

    while(x<3){

      cout<<"x="<<x;

    }


    #include <iostream>
    using namespace std;
    
    int main(){
        int x=1;
        while(x<11){//目前叫做死循环 必须要有一个不满足循环条件的时候 
            if(x%2==1)
                cout<<"x="<<x<<endl;
            x++;
        }
        return 0;
    }

    do while循环语句

    #include <iostream>
    using namespace std;
    
    int main(){
        int x=1;
        do{//目前叫做死循环 必须要有一个不满足循环条件的时候 
            if(x%2==1)
                cout<<"x="<<x<<endl;
            x++;
        }while(x<11);
        return 0;
    }

    do while与while语句的区别是什么?

    (1)在do while循环结束之后必须写 ; 分号

    (2)do while 必定会执行一次,而while不满足一次都不执行

    #include <iostream>
    using namespace std;
    
    int main(){
        int x=1;
        do{
            cout<<"x="<<x<<endl;
            x++;
        }while(x<1);
        return 0;
    }
    #include <iostream>
    using namespace std;
    
    int main(){
        int x=1;
        while(x<1){
            cout<<"x="<<x<<endl;
            x++;
        }
        return 0;
    }
  • 相关阅读:
    JRE、JDK和JVM之间的关系
    操作系统——CPU、计算机的构成
    为什么要用Java泛型
    【docker】 centos7 下 使用docker 安装 LNMP
    【docker】 centos7 安装docker
    【laravel5.6】 IlluminateDatabaseQueryException : SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes
    【truffle】Error: `truffle init` no longer accepts a project template name as an argument.
    【宝塔面板】pm2 安装没反应问题
    【node.js】】MSBUILD : error MSB3428: 未能加载 Visual C++ 组件“VCBuild.exe”。
    【git】 linux 环境安装git
  • 原文地址:https://www.cnblogs.com/qingyundian/p/7594942.html
Copyright © 2011-2022 走看看