#includeusing namespace std; // ******************** 选择结构的学习 ********************* // int main() { // 选择结构单行 if 语句 int score1; cout << "请输入您的分数" << endl; cin >> score1; cin.get(); cout << "您输入的分数为 : " << score1 << endl; if ( score1 > 600) // if 条件之后不加 “ ; ”,否则不具有选择,会顺序执行 { cout << " 恭喜您成功考入一本大学 ! " << endl; } // 选择结构多行 if 语句 int score2; cout << "请输入您的分数" << endl; cin >> score2; cin.get(); cout << "您输入的分数为 : " << score2 << endl; if (score2 > 600) // if 条件之后不加 “ ; ”,否则不具有选择,会顺序执行 { cout << " 恭喜您成功考入一本大学 ! " << endl; } else { cout << " 您未考取一本大学 " << endl; } // 选择结构种多条件选择 if 语句 int score3; cout << "请输入您的分数" << endl; cin >> score3; cin.get(); cout << "您输入的分数为 : " << score3 << endl; if (score3 > 600) // if 条件之后不加 “ ; ”,否则不具有选择,会顺序执行 { cout << " 恭喜您成功考入一本大学 ! " << endl; } else if (score3 > 500) { cout << " 恭喜您成功考入二本大学 !"; } else if (score3 > 400) { cout << " 恭喜您成功考入三本大学 !"; } else { cout << " 很遗憾 ,您未能被本科大学录取, 请再接再厉! " << endl; } // 嵌套 if 语句 int score4; cout << "请输入您的分数" << endl; cin >> score4; cout << " 您输入的分数是 : " << score4 << endl; if (score4 > 600) { if (score4 > 700) { cout << "恭喜考入北京大学!" << endl; } else if (score4 > 650) { cout << "恭喜考入清华大学" << endl; } else { cout << "恭喜考入中国人民大学" << endl; } } else if (score4 > 500) { cout << "恭喜您成功考入二本大学 !" << endl; } else if (score4 > 400) { cout << "恭喜您考入三本大学" << endl; } else { cout << " 很遗憾 ,您未能被本科大学录取, 请再接再厉! " << endl; } cin.get(); // 三只小猪称体重 int num1 ,num2 , num3; cout << " 请输入第一只小猪的体重 : " << endl; cin >> num1; cout << " 请输入第二只小猪的重量 :" << endl; cin >> num2; cout << " 请输入第三只小猪的重量 :" << endl; cin >> num3; if ( num1 > num2) { if (num1 > num3) { cout << "第一只小猪最重" << endl; } else { cout << "第三只小猪最重" << endl; } } else if (num2 > num3) { cout << "第二只小猪最重" << endl; } else { cout << "第三只小猪最重" << endl; } cin.get(); // 两数比较大小,大的输出 int a, b, c; cout << "请输入参与比较的两个数字:" << endl; cin >> a >> b; cout << "您输入的两个数字是:" << a << " t" << b << endl; (a > b) ? (c = a) : (c = b); cout << "较大的数字是 : " << c << endl; cin.get(); int score; cout << "请输入您对该电影的评价分数:" << endl; cin >> score; cout << "您对该电影的评价分数为: " << score << endl; switch ( score ) { case 10 : cout << "经典" << endl; break; // 没有 break ,后续语句会相继执行 case 9 : cout << "好" << endl; break; case 8: cout << "一般" << endl; break; default: cout << "差" << endl; } cin.get(); // if 和 switch 区别 // switch 缺点 : 判断时候只能是整型或者字符型,不可以是一个区间 // switch 优点 : 结构清晰,执行效率高 cin.get(); return 0; }



