如何不断提问,直到满足c中必须满足条件的要求
How to ask question continuously until it meets requirements of having to meet a condition in c
我正在在线使用 cs50 IDE 所以我也在顶部使用 cs50 库。我试图让它一直问“你还好吗?”这个问题。直到你说“y”或“n”(大写或小写)。
#include <cs50.h>
#include <stdio.h>
int main(void)
{
char answer;
do
{
char answer = get_char("are you okay Y/N ");
if (answer == 'y' || answer == 'Y')
{
printf("you are okay");
}
else if (answer == 'n' || answer == 'N')
{
printf("you are not okay ");
}
}
while(answer != 'y' || answer != 'Y' || answer != 'n' || answer != 'N');
}
学习 C 语言的布尔逻辑时,常识会让你走得更远。那就是:养成说出你写的台词的习惯loud/in你的脑袋:
while(answer != 'y' || answer != 'Y' || answer != 'n' || answer != 'N');
翻译成英语:
"while the answer is not y
or it is not Y
or it is not..."
在这里你应该闻到鱼的味道。正确的 English/logic 宁愿是:
"while the answer is not y
and it is not Y
and..."
抛开常识,De Morgan's Laws 是学习任何编程之前非常必要的先决知识。
我正在在线使用 cs50 IDE 所以我也在顶部使用 cs50 库。我试图让它一直问“你还好吗?”这个问题。直到你说“y”或“n”(大写或小写)。
#include <cs50.h>
#include <stdio.h>
int main(void)
{
char answer;
do
{
char answer = get_char("are you okay Y/N ");
if (answer == 'y' || answer == 'Y')
{
printf("you are okay");
}
else if (answer == 'n' || answer == 'N')
{
printf("you are not okay ");
}
}
while(answer != 'y' || answer != 'Y' || answer != 'n' || answer != 'N');
}
学习 C 语言的布尔逻辑时,常识会让你走得更远。那就是:养成说出你写的台词的习惯loud/in你的脑袋:
while(answer != 'y' || answer != 'Y' || answer != 'n' || answer != 'N');
翻译成英语:
"while the answer is not
y
or it is notY
or it is not..."
在这里你应该闻到鱼的味道。正确的 English/logic 宁愿是:
"while the answer is not
y
and it is notY
and..."
抛开常识,De Morgan's Laws 是学习任何编程之前非常必要的先决知识。