说不出的问题!!!
以下是我自己写的一个简单的小小计算器,只能计算简单加减乘除,我预想中出现的结果应该是:choose +, -, *, /:+
First number: 6
Second number: 3
Answer: 6 + 3 = 9
choose +, -, *, /:
为什么实际上运行时,出现的结果却是:
choose +, -, *, /:+
First number: 6
Second number: 3
Answer: 6 + 3 = 9
choose +, -, *, /:
Error:Only +, -, *, / are available.Try again! (注明:为什么就多了这一行呢?这不是我想要的结果啊!原因何在?我总感觉是,当我输入第二个数字3的时候,按下回车键后,回车键的输入值被运用到了 printf("Choose +, -, *, or /:\t");
scanf("%c", &operator);
这两个个语句上,求高人给指点指点!)
以下是我自己写的一个雏形代码,估计有点难看!初学者,还望不要见笑:
#include<stdio.h>
int main ( void )
{
long a, b;
char operator;
long answer;
while (1)
{
printf("Choose +, -, *, or /:\t");
scanf("%c", &operator);
switch (operator)
{
case '+':
{
printf("First number:\t\t");
scanf("%ld", &a);
printf("Second number:\t\t");
scanf("%ld", &b);
printf("Answer:\t\t%ld %c %ld = %ld\n\n\n", a, operator, b, a+b);
break;
}
case '-':
{
printf("First number:\t\t");
scanf("%ld", &a);
printf("Second number:\t\t");
scanf("%ld", &b);
printf("Answer:\t%ld %c %ld = %ld\n\n\n", a, operator, b, a-b);
break;
}
case '*':
{
printf("First number:\t\t");
scanf("%ld", &a);
printf("Second number:\t\t");
scanf("%ld", &b);
printf("Answer:\t%ld %c %ld = %ld\n\n\n", a, operator, b, a*b);
break;
}
case '/':
{
printf("First number:\t\t");
scanf("%ld", &a);
printf("Second number:\t\t");
scanf("%ld", &b);
printf("Answer:\t%ld %c %ld = %ld\n\n\n", a, operator, b, a/b);
break;
}
default:
{
printf("\nError: Only +, -, *, / are available. Try again!\n\n\n");
}
} /*End of switch */
} /* End of while */
}