我是C++的新手,可以通过youtube和自己购买的一些书教自己。我无法为自己的一生弄清楚为什么第二个while语句不起作用。从数学的 Angular 来看,我认为它应该起作用。如果月份不是= 6月或7月,则执行if else语句。但是,即使我运行正确的答案,也总是运行“否则”。我觉得它与字符串有关,因此我在不使用或“||”的情况下对其进行了测试而且有效。因此,可能与组合字符串和或语句有关。关于将它们一起使用的研究也没有发现太多。谢谢您的帮助。
int main()
{
int year;
int day = 0;
string month = "x";
do
{
if (day == 0)
{
cout << "hello" << endl;
cout << "Please Enter your B-Day as Day, Month, Year" << endl;
cout << "day" << endl;
cin >> day;
}
else
{
cout << "Please enter a correct day" << endl;
cin >> day;
}
} while (day > 31 || day < 1);
do
{
if (month == "x")
{
cout << "Please enter the month you were born" << endl;
cin >> month;
}
else
{
cout << "Please Enter a correct Month." << endl;
cin >> month;
}
}
**while (month != "june" || month != "july");**
return 0;
}
最佳答案
如果您这样做:
while (month != "june" && month != "july");
或者,
while (!(month == "june" || month == "july"));
而不是:
while (month != "june" || month != "july");
即使使用多个逻辑或,您的程序也可以正常工作。
关于c++ - C++ While语句不适用于字符串和 “or”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62165667/