[英]I am having trouble implementing a “while loop” in C programming
对于我的课,我必须创建一个计算器。 在我编写的这段代码中,我尝试实现“ 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;
}
}
希望这能为您指明正确的方向。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.