C++语言语言讲解:多分支讲解 2018-02-23 11:59:03 | 作者:宋海青 | 阅读量:720

第四课 判断语句(多分支判断)

1、多分支

双分支和多分支都要保证多个条件是互斥的(互相排斥,不可能同时成立)

多分支的基本格式:

if(条件){

 

}else if(条件){

 

}else if(条件){

  

}…{

 

}else{

 

}

注意:最后的else不是必须的,可以没有!

 

例子:小明夏天去买冰棍,老板说买30个及以上1/个,20~291.5/个,10~192/个,10个以下2.5/个!

#include <iostream>

 

using namespace std;

 

int main(){

         int n;//数量

         double s;//价格

        

         cin>>n;//读入数量

        

         if(n >= 30){

                   s = n * 1;

         }else if(n >= 20 && n < 30){

                   s = n * 1.5;

         }else if(n >= 10 && n < 20){

                   s = n * 2;

         } else{

                   s = n * 2.5;

         }

        

         cout<<s<<endl;

        

}

 

 

上述代码可以简化:如果走到else if(n >= 20 && n < 30),就说明if不成立,也就是说,n是默认<30的!

#include <iostream>

 

using namespace std;

 

int main(){

         int n;//数量

         double s;//价格

        

         cin>>n;//读入数量

        

         if(n >= 30){

                   s = n * 1;

         }else if(n >= 20){

                   s = n * 1.5;

         }else if(n >= 10){

                   s = n * 2;

         } else{

                   s = n * 2.5;

         }

        

         cout<<s<<endl;

        

}

 

1717: 【入门】猜拳

1代表石头,2代表剪刀,3代表布

输入2个数代表2个人的出拳

判断第一个人输赢情况:赢了win  输了lose  平局tie

 

第一个人赢的组合:12    2   31

第一个人平局的组合:1 1    2 2   3 3

#include <iostream>

 

using namespace std;

 

int main(){

         int a,b;//代表出拳

         cin>>a>>b;

        

         //如果赢了

         if((a==1&&b==2) || (a==2&&b==3) || (a==3&&b==1)){

                   cout<<"win"<<endl;

         } else if(a == b){

                   cout<<"tie"<<endl;

         }else{

                   cout<<"lose"<<endl;

         }

        

}

 

1783: 【入门】小高考

89 90 60 90 -> 2

98 98 99 100 -> 5

98 100 99 59 -> Poor LanYangYang

 

统计学生四个科目的分数中,有几个A,几个D

#include <iostream>

 

using namespace std;

 

int main(){

    int a,b,c,d;//定义四个科目的成绩

         int x = 0;//统计A等级的数量

         int y = 0;//统计D等级的数量

        

         cin>>a>>b>>c>>d;

         //逐个科目判断

         //如果第一个科目>=90,则A等级自增,如果<60,则D等级数量自增

         if(a >= 90){

                   x = x + 1;

         }else if(a < 60){

                   y = y + 1;

         }

        

         if(b >= 90){

                   x = x + 1;

         }else if(b < 60){

             y = y + 1;

         }

        

         if(c >= 90){

                   x = x + 1;

         }else if(c < 60){

             y = y + 1;

         }

        

         if(d >= 90){

                   x = x + 1;

         }else if(d < 60){

             y = y + 1;

         }

        

         //如果考了4A,则多加一分

         if(x == 4){

                   x = x + 1;

         }

        

         //输出

         //如果有D,则没有高考资格,否则输出加分

         if(y > 0){

                   cout<<"Poor LanYangYang"<<endl;

         } else{

                   cout<<x<<endl;

         }

}

 

作业:

1013: 【入门】判断成绩等级

输入某学生成绩,如果86分以上(包括86分)则输出“VERY GOOD ,如果在6085之间的则输出“GOOD(包括6085),小于60的则输出“BAD”。

#include <iostream>

 

using namespace std;

 

int main(){

    int n;

    //读入要判断的成绩

    cin>>n;

    

    if(n >= 86){

     cout<<"VERY GOOD"<<endl;

         }else if(n >= 60){

                   cout<<"GOOD"<<endl;

         }else{

                   cout<<"BAD"<<endl;

         }

}

 

 

1018: 【入门】找出最经济型的包装箱型号

读入重量n,判断条件输出A B C D E

#include <iostream>

 

using namespace std;

 

int main(){

    int n;

    //读入货物的重量

    cin>>n;

    

    if(n < 10){

      cout<<"A"<<endl;

    }else if(n >= 10 && n < 20 ){

      cout<<"B"<<endl;

    }else if (n >= 20 && n < 40){

      cout<<"C"<<endl;

    }else if(n >= 40 && n < 50){

      cout<<"D"<<endl;

    }else if (n >= 50 && n < 80){

      cout<<"E"<<endl;

    }

}

1769: 【入门】汉译英

#include <iostream>

 

using namespace std;

 

int main(){

    int n;

    //读入要判断的数字

    cin>>n;

    

    if (n == 1){

        cout<<"one"<<endl;

    }else if (n == 2){

        cout<<"two"<<endl;

    }else if (n == 3){

        cout<<"three"<<endl;

    }else if (n == 4){

        cout<<"four"<<endl;

    }else if (n == 5){

        cout<<"five"<<endl;

    }else if (n == 6){

        cout<<"six"<<endl;

    }else if (n == 7){

        cout<<"seven"<<endl;

    }else if (n == 8){

        cout<<"eight"<<endl;

    }else if (n == 9){

        cout<<"nine"<<endl;

    }else{

        cout<<"out"<<endl; 

    }

}


在线观看云储存

快速搭建独立网校,免费观看
热门科目视频教程。

咨询金牌顾问

专属顾问免费咨询,全程配套跟踪
服务,让您学有所得。

实时更新视频教程

及时更新视频教程。

东方博宜教育咨询江苏有限公司 版权所有 苏ICP备12080391号
忘记密码?

关注的课程(多选)