<分区>
我正在使用字符串运行一系列 if
/else if
/else
语句,无论用户输入 if
总是返回 true:
cout << "Enter the element to look up: ";
cin >> elementLookup;
cout << endl << endl;
if (elementLookup == "H" || "Hydrogen" || "hydrogen") {
cout << "Atomic Number: " << H_NUMBER << endl << endl;
cout << "Atomic Mass: " << H_MASS << endl << endl;
}
else if (elementLookup == "He" || "Helium" || "helium") {
cout << "Atomic Number: " << HE_NUMBER << endl << endl;
cout << "Atomic Mass: " << HE_MASS << endl << endl;
}
else if (elementLookup == "Li" || "Lithium" || "lithium") {
cout << "Atomic Number: " << LI_NUMBER << endl << endl;
cout << "Atomic Mass: " << LI_MASS << endl << endl;
}
所有变量都已在代码的前面声明。出于某种原因,每次我运行这段代码时,无论用户输入如何,它都会说第一个 if 语句为真。我做错了什么”