判断一个字符是否等于Y或者N出现问题。

来源:百度知道 编辑:UC知道 时间:2024/05/24 20:00:36
怎么会出现两次“NOT”呢。
我是想这样的,检测一个字符是否等于Y和N,然后分别对Y和N作出响应。
VOID main()
{
char ctl;
scanf("%c",&ctl);
while(ctl!='Y'||ctl!='n')
{
printf("NOT\n");
scanf("%c",&ctl);
}
}

char ctl; 的字符比较要用 strcmp(..) 或strncmp(...) 函数,字符常量用双引号.

unsigned char ctl; 的字符比较 才可以用单引号'Y','y','N','n',(单引号这些值相当于整型值,才可以用 != 比较).

if (strcmp(ctl,"Y") == 0 || strcmp(ctl,"y") == 0) {
printf("This is Y or y");
} else {
printf("This is "no" \n");
};

VOID main()
{
char ctl;
scanf("%c",&ctl);
if(ctl!='Y'||ctl!='n')
{
printf("NOT\n");
scanf("%c",&ctl);
}
}