两个连续的减号是什么意思?

LB4229 2011-05-04 11:39:30
#include <iostream>
#include <conio.h>
using namespace std;

enum Weapons{Pistol, Shotgun, MachineGun, Bazooka, RocketLaucher};

int main(){
cout <<"Which weapon do you want? "<< endl;
cout <<"1) Pistal" <<endl;
cout <<"2) Shotgun" <<endl;
cout <<"3) Machinegun" <<endl;
cout <<"4) Bazooka" <<endl;
cout <<"5) RocketLaucher" <<endl <<endl;

int weapon_choice;
cin >> weapon_choice;

weapon_choice--;//这里带两个减号是什么意思?
switch(weapon_choice){
case Pistol:
cout << "You have chosen a Pistol";
break;

case Shotgun:
cout << "You have chosen a Shotgun";
break;
case MachineGun:
cout << "You have chosen a MachineGun";
break;
case Bazooka:
cout << "You have chosen a Bazooka";
break;

case RocketLaucher:
cout << "You have chosen a RocketLaucher";
break;

}
getch();
return 0;

}


问下代码里两个连续的减号是什么意思?
...全文
965 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
LB4229 2011-05-05
  • 打赏
  • 举报
回复
嗯,明白了,谢谢楼上的,刚才结帖没发现你的回复,不好意思,这次不能给你分了,谢谢你哈
pathuang68 2011-05-04
  • 打赏
  • 举报
回复
--的含义,1楼说得已经非常清楚了。

enum Weapons{Pistol, Shotgun, MachineGun, Bazooka, RocketLaucher};
// 这个枚举,让:Pistol = 0, Shotgun = 1, MachineGun = 2, Bazooka = 3, RocketLauncher = 4

int weapon_choice;
cin >> weapon_choice;

weapon_choice--; // 如果你输入的是1, 那么这句话执行完成后weapon_choice就等于0

switch(weapon_choice){
case Pistol: // 所以如果你前面输入的是1,就会选择执行这个case
cout << "You have chosen a Pistol";
break;

case Shotgun: // 所以如果你前面输入的是2,就会选择执行这个case
cout << "You have chosen a Shotgun";
break;
case MachineGun: // 所以如果你前面输入的是3,就会选择执行这个case
cout << "You have chosen a MachineGun";
break;
case Bazooka: // 所以如果你前面输入的是4,就会选择执行这个case
cout << "You have chosen a Bazooka";
break;

case RocketLaucher: // 所以如果你前面输入的是5,就会选择执行这个case
cout << "You have chosen a RocketLaucher";
break;

}

LB4229 2011-05-04
  • 打赏
  • 举报
回复
哦。。。。确实如此,谢谢啦
hitaka85 2011-05-04
  • 打赏
  • 举报
回复
enum Weapons{Pistol, Shotgun, MachineGun, Bazooka, RocketLaucher};
Pistol=0,Shotgun=1.....
ljt3969636 2011-05-04
  • 打赏
  • 举报
回复
要匹配enum Weapons{Pistol, Shotgun, MachineGun, Bazooka, RocketLaucher};这里枚举类型默认从0开始等价于enum Weapons{0, 1, 2, 3, 4};
而你输入的从1开始:

cout <<"1) Pistal" <<endl;
cout <<"2) Shotgun" <<endl;
cout <<"3) Machinegun" <<endl;
cout <<"4) Bazooka" <<endl;
cout <<"5) RocketLaucher" <<endl <<endl;

所以减1啊
LB4229 2011-05-04
  • 打赏
  • 举报
回复
这里为什么要减一呀?就用了一次weapon_choice作为switch判断的依据呀
ljt3969636 2011-05-04
  • 打赏
  • 举报
回复
weapon_choice--;相当于weapon_choice=weapon_choice-1;
完整滴说int i=weapon_choice--;相当于宣布int i=weapon_choice;然后weapon_choice=weapon_choice-1;
int i=--weapon_choice;相当于先 weapon_choice=weapon_choice-1;然后 int i=weapon_choice;

64,651

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧