...
void Character::classSelect()
{
cout << "---- Choose the Class of your Hero ----\n";
cout << "---- (1) -- Warrior -------------------\n";
cout << "---- (2) -- Mage ----------------------\n";
cout << "---- (3) -- Battlemage ----------------\n";
cout << "---------------------------------------\n";
int c;
cin >> c;
switch (c)
{
case 1: m_class = "Warrior", hp = 35, physDmg = 5, magicDmg = 1;
case 2: m_class = "Mage", hp = 20, physDmg = 1, magicDmg = 10;
case 3: m_class = "Battlemage", hp = 25, physDmg = 7, magicDmg = 7;
default: m_class = "Warrior", hp = 35, physDmg = 5, magicDmg = 1;
cout << "You chose : " << m_class << endl;
}
}
...
void Character::print()
{
cout << getCharClass() << " (HP:" << getHp() << "/DAMAGE-PHYICAL:" <<
getPhysDmg() << "/DAMAGE-MAGICAL:" << getMagicDmg() << ")\n";
}
...
int main()
{
Character player;
player.classSelect();
player.print();
return 0;
}
无论我用'cin'输入什么,似乎每次打印默认情况。例如,如果我输入2,它仍会打印“Warrior(HP .....”。 任何帮助都会得到赞赏。
答案 0 :(得分:0)
你应该在每种情况下添加休息时间......
switch (c)
{
case 1:
m_class = "Warrior", hp = 35, physDmg = 5, magicDmg = 1;
break;
case 2:
m_class = "Mage", hp = 20, physDmg = 1, magicDmg = 10;
break;
case 3:
m_class = "Battlemage", hp = 25, physDmg = 7, magicDmg = 7;
break;
default: m_class = "Warrior", hp = 35, physDmg = 5, magicDmg = 1;
cout << "You chose : " << m_class << endl;
}
答案 1 :(得分:0)
你需要使用
break;
要结束的案例指令:您当前的代码将一直运行到循环结束并且每次都运行默认值
switch (c)
{
case 1: m_class = "Warrior", hp = 35, physDmg = 5, magicDmg = 1; break;
case 2: m_class = "Mage", hp = 20, physDmg = 1, magicDmg = 10; break;
case 3: m_class = "Battlemage", hp = 25, physDmg = 7, magicDmg = 7; break;
default: m_class = "Warrior", hp = 35, physDmg = 5, magicDmg = 1; break;
cout << "You chose : " << m_class << endl;
}
应该给你你想要的东西
答案 2 :(得分:0)
switch
的行为类似于可转位goto
。 case
- s只是“标签”,当“点击”下一个case
时,代码只会落空。如果要结束某个开关案例,则必须使用break
终止它。像这样:
switch (c)
{
case 1: m_class = ...; break;
case 2: m_class = ...; break;
default: ...;
}