C 如何在不退出黑屏的情况下进行另一次计算?

C 如何在不退出黑屏的情况下进行另一次计算?,c,switch-statement,C,Switch Statement,这是我的代码:switch语句 #include <stdio.h> #include <conio.h> void main() { float a,b,ans; int code; printf("enter two no\n"); scanf("%f%f",&a,&b); printf("select an expression \n1-addition \n2-substraction \n3multipl

这是我的代码:switch语句

#include <stdio.h>
#include <conio.h>
void main()
{
    float a,b,ans;
    int code;
    printf("enter two no\n");
    scanf("%f%f",&a,&b);

    printf("select an expression \n1-addition \n2-substraction \n3multiplication \n4-divide\n ");
    scanf("%d",&code);

switch(code)
{
    case 1:ans=a+b;
        printf("%f\n",ans);
    break;
    case 2:ans=a-b;
        printf("%f\n",ans);
    break;
    case 3:ans=a*b;
        printf("%f\n",ans);
    break;
    case 4:ans=a/b;
        printf("%f\n",ans);
    break;

}

getch();
}
现在,在进行一次计算后,让我们假设1-加法,我想再次进行另一次计算,而不退出黑屏。 如何做到这一点?
如果我必须设置一个循环,那么在哪里以及如何设置,请解释。

以下代码将对给定输入执行所有操作,直到您选择退出。但是,如果要对不同的值执行操作,可以在while循环中包含第7行和第8行

while(1){
printf("Press 0 to exit"); 
switch(code)
{
case 1:ans=a+b;
    printf("%f\n",ans);
break;
case 2:ans=a-b;
    printf("%f\n",ans);
break;
case 3:ans=a*b;
    printf("%f\n",ans);
break;
case 4:ans=a/b;
    printf("%f\n",ans);
case 0:
exit();


break;

}
}
#include <stdio.h>
#include <conio.h>
void main()
{
    float a,b,ans;
    int code;
    printf("enter two no\n");
    scanf("%f%f",&a,&b);
    while(1)
    {
        printf("select an expression \n1-addition \n2-substraction \n3multiplication \n4-divide \n5-Exit\n");
        scanf("%d",&code);
        switch(code)
        {
            case 1:
                ans=a+b;
                printf("%f\n",ans);
                break;
            case 2:
                ans=a-b;
                printf("%f\n",ans);
                break;
            case 3:
                ans=a*b;
                printf("%f\n",ans);
                break;
            case 4:
                ans=a/b;
                printf("%f\n",ans);
                break;
        }

        if(code == 5)
        return;
 }

    getch();
}

请阅读标题为while loops的第二章,以及你在等待什么。。问题是,你需要学习如何编写程序。那恐怕意味着要努力工作。它通常意味着阅读书籍和查找资料,然后尝试一些第一次知道它可能不会正常工作的资料,然后测试它,然后调试它并再次测试。这就是软件工程的工作原理。