|
为什么以下程序要循环两次:
[code:1]#include<stdio.h>
#define BLACK "\33[;3m"
#define BLUE "\33[;34m"
#define CYAN "\33[;36m"
#define RED "\33[;31m"
#define PURPLE "\33[;35m"
#define YELLOW "\33[1;33m"
int main(void)
{
int rec;
rec=(int)NULL;
printf("please choose which color you like:\n");
printf("blAck, Blue, Cyan, Red, Purple, Yellow\n");
while(!(rec=='A'||rec=='B'||rec=='C'||rec=='R'||rec=='P'||rec=='Y'))
{
fflush(stdin);
while(rec<'a'||rec>'z'){
printf("Please input a character between 'a' and 'z'\n");
scanf("%c",&rec);
}
printf("Character you input is **%c**\n",rec);
rec=toupper(rec);
switch(rec)
{
case 'A' : printf(BLACK"Hello, world!\n");break;
case 'B' : printf(BLUE"Hello, world!\n");break;
case 'C' : printf(CYAN"Hello, world!\n");break;
case 'R' : printf(RED"Hello, world!\n");break;
case 'P' : printf(PURPLE"Hello, world!\n");break;
case 'Y' : printf(YELLOW"Hello, world!\n");break;
default : printf("Sorry,but you have input an illegel character!\n");
}
}
return ;
}[/code:1]
我想可能是getchar()的问题。例如第一次输入1<center>后就会出现输出两次Please input a character between 'a' and 'z'。请问这是什么原因?应该如何解决?谢谢。 |
|