简体   繁体   English

我在C编程中无法实现“ while循环”

[英]I am having trouble implementing a “while loop” in C programming

For my class, I had to create a Calculator. 对于我的课,我必须创建一个计算器。 In this code that I wrote, I tried to implement the "while loop" so that the program would run continuously and I also wanted it to end when the user typed in XO which is short for exit operation. 在我编写的这段代码中,我尝试实现“ while循环”,以便程序可以连续运行,并且我还希望当用户键入XO(它是退出操作的缩写)时结束程序。 I kept getting errors on my IDE when I tried to run the program. 尝试运行程序时,我在IDE上始终出现错误。 Can anyone help me figure the rest of this out or help me find the resources to figure this out. 谁能帮助我解决其余问题,或者帮助我找到解决问题的资源。 Thanks 谢谢

very respectfully, 非常恭敬地

#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;
}

I'll give you a little help here, though you still have some work left to get this working. 我会在这里给您一点帮助,尽管您还有一些工作要做。

A while loop that will run indefinitely is made like this: 这样会无限期地运行while循环:

while(1) {
    //Your code here
}

If you want to stop that loop when the user enters X , try this: 如果要在用户输入X时停止该循环,请尝试以下操作:

while(1) {
    scanf("%c" ,&letter);
    if(letter == 'X') {
        printf("Goodbye!");
        break;
    }
}

Hopefully that points you in the right direction. 希望这能为您指明正确的方向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM