C++中switch和枚举结合使用:
#include "iostream"
using namespace std;
// 创建一个枚举类型常量, 实际对应的是0~2
enum {red, orange, yellow};
int main() {
cout << "Enter color code (0~2)";
int code;
cin >> code;
while (code >= red && code <= yellow)
{
switch(code)
{
case red :
cout << "red" << endl;
break;
case orange:
cout << "orange" << endl;
break;
case yellow:
cout << "yellow" << endl;
break;
default :
cout << "unkonw" << endl;
}
cout << "Enter color code (0~2)";
cin >> code;
}
cout << "end" << endl;
return 0;
}
输出结果:
注意c++中switch语句只能对整数值进行处理(java 高版本可以对string进行处理), 而枚举实际也是int型变量, switch将int和枚举进行比较的时候, 会将枚举提升为int, 所以枚举也可进行处理.
再看一个while循环的简单使用
注意while条件判断中的语句写法
#include "iostream"
using namespace std;
const int MAX = 5;
int main() {
int golf[MAX];
cout << "Please enter your g