在 C 中要求用户重复该程序或退出

yay*_*aya -5 c printf

在这个非常基本的程序中,要求用户输入两个数字,然后程序将这些数字相加。我想在最后询问用户是否想再次重复该程序或退出该程序!例如,如果他/她按 y,程序将要求用户输入两个数字,否则程序将关闭。怎么做 ?

main(){
float x,y,sum;
printf ("Enter the first number:");
scanf ("%f",&x);
printf ("Enter the second number:");
scanf ("%f",&y);
sum=x+y;
printf ("The total number is:%f",sum);
}
Run Code Online (Sandbox Code Playgroud)

Tan*_*ikh 5

main(){
    float x,y,sum;
    char ch;

    do{
    printf ("Enter the first number:");
    scanf ("%f",&x);
    printf ("Enter the second number:");
    scanf ("%f",&y);
    sum=x+y;
    printf ("The total number is:%f",sum);
    printf ("Do you want to continue: y/n");
    scanf (" %c", &ch);
    } while(ch == 'y');
    }
Run Code Online (Sandbox Code Playgroud)

或者你也可以尝试这个:

main(){
    float x,y,sum;
    char ch;

    do{
    printf ("Enter the first number:");
    scanf ("%f",&x);
    printf ("Enter the second number:");
    scanf ("%f",&y);
    sum=x+y;
    printf ("The total number is:%f",sum);
    printf ("Do you want to continue: y/n");
    ch = getchar();
    getchar();
    } while(ch == 'y');
    }
Run Code Online (Sandbox Code Playgroud)