use*_*717 5 c if-statement while-loop
我浪费时间思考为什么这个程序运行不正常,没有成功.它始终打印"角色是一个特殊的符号".
#include <stdio.h>
int main( void )
{
char character;
printf( "Please type any character and I will tell you what type it is:\n" );
while( 1 )
{
scanf( "%c", &character);
if( character >= 65 && character <= 90 )
printf( "The character is A-Z\n" );
else if( character >= 97 && character <= 122 )
printf( "The character is a-z\n" );
else if( character >= 48 && character <= 57 )
printf( "The character is 0-9\n" );
else if(( character >= 0 && character <= 47 ) ||
( character >= 58 && character <= 64 ) ||
( character >= 91 && character <= 96 ) ||
( character >= 123 && character <= 127 ))
printf( "The character is a special symbol\n" );
}
}
Run Code Online (Sandbox Code Playgroud)
运行示例
Please type any character and I will tell you what type it is:
4
The character is 0-9
The character is a special symbol
Run Code Online (Sandbox Code Playgroud)
我注意到当我删除while循环时没有发生,但我不明白为什么,我想要那个循环.
你scanf应该是这样的:
scanf(" %c", &character);
Run Code Online (Sandbox Code Playgroud)
你是The character is a special symbol因为scanf也在读书\n.