【发布时间】:2013-11-05 03:16:27
【问题描述】:
我正在尝试做一个简单的 do while 循环,其中除 'y' 或 'n' 之外的任何字母都无效并且循环重复。有谁知道为什么这个循环总是评估为假?即使输入了有效字符?
char user_response[2];
do {
printf("\nDo you want to process another range (y or n): ");
scanf("%1s", user_response);
user_response[0] = tolower(user_response[0]);
}
while (user_response[0] != 'y' || user_response[0] != 'n');
return user_response[0];
【问题讨论】:
-
如果响应为
'y',则不等于'n',反之亦然。因此,您的一个或两个表达式将始终为真。 -
您的循环测试一个虚构的字符,它同时是“y”和“n”。它不会接受任何不是'y'的东西,它也不会接受任何不是'n'的东西。