【发布时间】:2015-01-12 04:17:43
【问题描述】:
一个while语句可以同时包含三个条件吗?
例如:
cout << "\n\nHow would you like your parcel shipped?\n (please type standard, express"
" or same day)" << endl;
cin >> method;
while (method != ("standard" && "express" && "same day"))
{
cout << "invalid input: please follow the instructions carefully.." << endl;
cout << "\n\nHow would you like your parcel shipped?\n (please type 'standard',"
" 'express' or 'same day')" << endl;
cin >> method;
}
我问的原因是,当我运行这段代码时,它进入了一个无限循环,我无法理解为什么。
【问题讨论】:
-
这里的
&&运算符有指向字符串的指针作为参数。 -
当答案出现时,想想你的表达式
("standard" && "express" && "same day")以及该表达式产生了什么值。 -
“它进入了一个无限循环,我无法理解为什么。” 好吧,这里的当前答案都没有正确解释这一点。 (而真正尝试这样做的人删除了他们的答案:-()。
-
什么是
method?char[*]、char*、std::string? -
顺便说一句,别那么
endl-happy。在大多数情况下,标准换行符就足够了,更简单,性能更高。
标签: c++ while-loop