第1个回答 2012-03-18
enum GameResult {WIN,LOSE,TIE,CANCEL};//定义一个枚举类型
int main()
{
GameResult result;
enum GameResult omit=CANCEL;//枚举型变量omit,默认为cancel
//下面的也就是按顺序显示一下各个结果罢了
for(int count=WIN; count<=CANCEL;count++)//从win到cancel,也即从0到3
{
result=GameResult(count);//int型强制转换为枚举类型,否则报错(int型无法隐式转换为枚举型如果记得不错)!
if(result==omit)//结果为omit(=cancel)
cout<<"the game was cancelled"<<endl;//游戏取消
else{
cout<<"the game was played";//游戏结束
if(result==WIN)//结果为赢
cout<<"and we won!";
if(result==LOSE)//结果为输
cout<<"and we lost.";
cout<<endl;
}
}
return 0;
}
第2个回答 2012-03-19
#include<iostream>
using namespace std;
enum GameResult {WIN,LOSE,TIE,CANCEL};/*定义枚举名GameResult,常量为"WIN,LOSE,TIE,CANCEL".
其中系统默认WIN=0,LOSE=1,TIE=2,CANCEL=3*/
int main()
{
GameResult result;
enum GameResult omit=CANCEL;//omit指向GameResult中的CANCEL
for(int count=WIN; count<=CANCEL;count++)//意思即for(int count=0; count<=3;count++)
{
result=GameResult(count);//result指向GameResult中的第count个变量,如result=GameResult(2)=TIE
if(result==omit)
cout<<"the game was cancelled"<<endl;
else{
cout<<"the game was played";
if(result==WIN)
cout<<"and we won!";
if(result==LOSE)
cout<<"and we lost.";
cout<<endl;
}
}
return 0;
}
/*还是希望楼主自己去看一下枚举*/
第3个回答 2012-03-18
enum GameResult {WIN,LOST,TIE,CANCEL};是枚举。if(result==LOSE)是LOSE.当然只有等于LOSE才能编译通过。