我是一個新手學習代碼,所以這可能很簡單,但我不明白髮生了什麼。C - while條件掛在條件
當我嘗試運行以下代碼時,它似乎掛在while
循環條件中,並且在此之後不執行任何代碼。 !=
運營商是否不適合此?
我也嘗試了do {} while
,一切運行正常,但只要我將運算符設置爲'E'
,條件似乎仍然掛在那裏,並沒有脫離循環。
#include <stdio.h>
int main(void) {
float n, content;
char operator;
n = content = 0;
operator = 'a';
printf("Begin Calculations:\n");
while (operator != 'E');
{
scanf("%f %c", &n, &operator);
switch (operator) {
case 's':
case 'S':
content = n;
printf("= %f\n", content);
break;
case '+':
content = content + n;
printf("= %f\n", content);
break;
case '-':
content = content - n;
printf("= %f\n", content);
break;
case '*':
content = content * n;
printf("= %f\n", content);
break;
case '/':
content = content/n;
printf("= %f\n", content);
break;
case 'e':
case 'E':
printf("End of calculations\n");
operator == 'E';
break;
default:
printf("Invalid input\n");
break;
}
}
return 0;
}
'運營商== 'E';'會不會將e'轉換爲E'。請啓用編譯器警告! –