while语句中的嵌套If语句总是选择第一个If语句



首先,我对c++还是个新手。我正在做一个项目,只是为了好玩,但一直遇到一个问题(见下面的代码),无论我的输入词是什么,代码都会将statRollArray值分配给智慧int。我试过所有不同的输入法,但它总是表现得好像我输入了"智慧"。为什么会这样?任何帮助都是感激的!

while (charisma == 0 || strength == 0 || dexterity == 0 || intelligence == 0 || wisdom == 0 || consitution == 0) {
cout << "Where would you like to assign your roll of " << statRollArray[chooseStatNumber] << "?" << endl;
if (dexterity == 0) {
cout << "Dexterity";
}
if (intelligence == 0) {
cout << " Intelligence";
}
if (strength == 0) {
cout << " Strength";
}
if (wisdom == 0) {
cout << " Wisdom";
};
if (charisma == 0) {
cout << " Charisma";
}
if (consitution == 0) {
cout << " Constitution"
<< endl;
}
string choice = "";
cin >> choice;
if (choice == "wisdom" || "Wisdom" || "Dexterity" || "dexterity" || "Strength" || "strength" || "Intelligence" || "intelligence"
|| "Charisma" || "charisma" || "Constitution" || "constitution") {
if (choice == "wisdom" || "Wisdom") {
if (wisdom == 0) {
wisdom = statRollArray[chooseStatNumber];
chooseStatNumber += 1;
continue;
}
}
else {
cout << "Wisdom already has been assigned!" << endl;
continue;
}
if (choice == "Dexterity" || "dexterity") {
if (dexterity == 0) {
dexterity = statRollArray[chooseStatNumber];
chooseStatNumber += 1;
continue;
}
}
else {
cout << "Dexterity already has been assigned!" << endl;
continue;
}
if (choice == "strength" || "Strength") {
if (strength == 0) {
strength = statRollArray[chooseStatNumber];
chooseStatNumber += 1;
continue;
}
}
else {
cout << "Strength already has been assigned!" << endl;
continue;
}
if (choice == "intelligence" || "Intelligence") {
if (intelligence == 0) {
intelligence = statRollArray[chooseStatNumber];
chooseStatNumber += 1;
continue;
}
}
else {
cout << "Intelligence already has been assigned!" << endl;
continue;
}
if (choice == "charisma" || "Charisma") {
if (charisma == 0) {
charisma = statRollArray[chooseStatNumber];
chooseStatNumber += 1;
continue;
}
}
else {
cout << "Charisma already has been assigned!" << endl;
continue;
}
if (choice == "constitution" || "Constitution") {
if (consitution == 0) {
consitution = statRollArray[chooseStatNumber];
chooseStatNumber += 1;
continue;
}
}
else {
cout << "Constitution already has been assigned!" << endl;
continue;
}
}
else {
cout << "Please choose one of the listed stats." << endl;
continue;
}
}

为什么?

条件表达式没有按照您的想法求值。例如:

if (choice == "wisdom" || "Wisdom" || "Dexterity" || "dexterity" || "Strength" || "strength" || "Intelligence" || "intelligence"
|| "Charisma" || "charisma" || "Constitution" || "constitution") 

首先计算choice == "wisdom"。如果为真,则执行if子句。但如果它为假,则计算下一个||项,即"Wisdom"(而不是choice== "Wisdom"),它不是null,因此被认为是真。所以你的条件永远为真。

你必须明确你想比较什么:

if (choice == "wisdom" || choice == "Wisdom" || choice == "Dexterity" ...)  

有没有更简单的方法?

一种更简单的方法是将长列表打包到容器中,例如set,并使用简单的查找:

set<string> possible_terms{"wisdom", "Wisdom", "Dexterity",... }; 
if (possible_terms.count(choice))
... // found 
else ... // not found

另一种简化是将输入小写,并使用小写单词进行所有比较。这将更加健壮,并且避免您必须预见代码中每一个可能的大小写组合:

for (auto&c:choice) 
c = tolower(c); 
...
if (choice == "wisdom" ) ... // works with "wisdom", "Wisdom", "WISDOM", "wiSDom" etc... 

相关内容

  • 没有找到相关文章

最新更新