0
我的代码: 我是一个初学者 - 所以在我身上很容易 因此,以下代码中的switch语句执行多个case的语句。我似乎无法找到问题。一切似乎都是正确的。 检查输出图像和我的代码,我需要帮助。switch语句执行超过一个case - C编程
//write a program that acts as a simple "printing" calculator
//enter expressions in the format "operator number"
//include operators to 'set accumulator' and 'end execution'
#include<stdio.h>
int main()
{
float number, accumulator;
char operator;
printf("Begin Calculations: (Enter 'S' operator to set the accumulator and 'E' operator to end execution)\n");
printf("-------------------\n\n");
while(operator != 'E')
{
scanf("%c%f", &operator, &number);
switch(operator)
{
case 'S':
accumulator = number;
break;
case '+':
accumulator += number;
break;
case '-':
accumulator -= number;
break;
case '*':
accumulator *= number;
break;
case '/':
accumulator /= number;
break;
case 'E':
printf("End of Calculations.\n");
break;
default:
printf("Enter proper Expression.\n");
break;
}
printf("= %f\n", accumulator);
}
return 0;
}
请勿发布图片的文字! **总是**检查可能遇到与您的程序相关的错误的功能的结果。 – Olaf
你的'scanf'正在读入'operator' *(并且没有在循环中将一个浮点数读入'number' * **)**)** – pmg