我的代码有问题,在编译并运行代码后会出现两次输出错误输出。
我的任务是重复输入数字/字母/大写并相应地输出结果。至于while循环,我相信我必须放while(1)
。
#include <stdio.h>
int main()
{
char c;
int num;
printf("\nEnter a character: ");
scanf("%c", &c);
if ((c >= 'a' && c <= 'z'))
printf("%c\nIt is an alphabet.", c);
if ((c >= 'A' && c <= 'Z'))
printf("%c\n It is a capital alphabet.", c);
if (c <= '1' ||c >= '1')
printf("\nIt is a numeric");
else
printf("error");
return 0;
}
答案 0 :(得分:1)
你需要这样的工作:
#include <stdio.h>
int main()
{
char c;
int num, int general=0;
printf("\nEnter a character: ");
scanf("%c", &c);
if ((c >= 'a' && c <= 'z')) {
printf("%c\nIt is an alphabet.", c);
general++;
}
if ((c >= 'A' && c <= 'Z')){
printf("%c\n It is a capital alphabet.", c);
general++;
}
if (c <= '1' ||c >= '1') {
printf("\nIt is a numeric");
general++;
}
if (general==0)
printf("error");
return 0;
}
或者您可以在“捕捉和切换”中使用。
答案 1 :(得分:0)
需要进行三项更改。
首先使用else if
其次要测试数字使用c >= '0' && c <= '9'
。
最后但并非最不重要:使用getchar()
作为循环的最后一个语句来吃缓冲区中的\n
字符而不是scanf