我花了好几个小时思考为什么这个程序运行不正常,没有成功。它总是打印“该字符是一个特殊符号”。
#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" );
}
}
运行示例
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
我注意到当我删除 while 循环时它不会发生,但我不明白为什么,我想要那个循环。
最佳答案
你的 scanf
应该是这样的:
scanf(" %c", &character);
你得到 The character is a special symbol
因为 scanf
也在读取 \n
。
关于c - 带有 "if"的循环无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25819002/