尝试在 C 中编写一个简单的计算器。如果未输入字母“A”、“B”、“C”或“D”,我会卡住程序终止,否则继续。即使我输入了一个有效的字符,它也永远不会继续。
#include <stdio.h>
#include <stdlib.h>
int main()
{
char letter;
float num1,num2;
printf("What operation would you like to perform?\n\tA) Addition\n\tB) Subtraction\n\tC) Multiplication\n\tD) Division\n\n");
scanf("%c", &letter);
if (letter != 'A' || letter != 'B' || letter != 'C' || letter != 'D')
printf("Not a valid operation\n");
return 1;
printf("Please enter first number: ");
scanf("%f", &num1);
printf("Please enter second number: ");
scanf("%f", &num2);
if (letter == 'A' || letter == 'a')
printf("The sum of %f and %f, is %f\n", num1, num2, num1 + num2);
else if (letter == 'B' || letter == 'b')
printf("The difference of %f and %f, is %f\n", num1, num2, num1 - num2);
else if (letter == 'C' || letter == 'c')
printf("The product of %f and %f, is %f\n", num1, num2, num1 * num2);
else if (letter == 'D' || letter == 'd')
printf("The quoation of %f and %f, is %f\n", num1, num2, num1 / num2);
else
printf("The operation was not valid");
return 0;
}
谢谢您的帮助。