对于我的类(class),我必须创建一个计算器。在我编写的这段代码中,我尝试实现“while循环”,以便程序能够连续运行,并且我还希望它在用户输入XO(退出操作的缩写)时结束。当我尝试运行该程序时,IDE 上不断出现错误。谁能帮我解决剩下的问题,或者帮我找到解决这个问题的资源。谢谢
非常尊敬,
#include <stdio.h>
#include <stdlib.h>
int main()
{
char letter;
float num1,num2;
printf("What operation do you want to do\n\tA)Addition\n\tB)Subtraction
\n\tC)Multiplication\n\tD)Division\n?");
scanf("%c" ,&letter);
printf("Please enter a number :");
scanf("%f" ,&num1);
printf("Please enter another number :");
scanf("%f" ,&num2);
if (letter=='A' || letter=='a')
printf("The sum of %.2f and %.2f is %.2f" ,num1,num2,num1+num2);
else if (letter=='B' || letter=='b')
printf("The difference of %.2f and %.2f is %.2f" ,num1,num2,num1-
num2);
else if (letter=='C' || letter=='c')
printf("The product of %.2f and %.2f is %.2f ,num1,num2,num1*num2");
else if(letter=='D' || letter=='d')
printf("The quotient of %.2f and %.2f is %.2f
,num1,num2,num1/num2");
else
printf("You entered an invalid character.");
return 0;
}
最佳答案
我将为您提供一些帮助,尽管您还需要做一些工作才能使其正常工作。
无限期运行的 while 循环如下所示:
while(1) {
//Your code here
}
如果您想在用户输入 X
时停止该循环,请尝试以下操作:
while(1) {
scanf("%c" ,&letter);
if(letter == 'X') {
printf("Goodbye!");
break;
}
}
希望这能为您指明正确的方向。
关于c - 我在 C 编程中实现 "while loop"时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34033521/