C语言 要求用户重复程序或退出 C

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34941692/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 12:19:28  来源:igfitidea点击:

Ask user to repeat the program or exit in C

cprintf

提问by yaya

In this very basic program which ask the user to input two numbers, and then the program will sum these numbers together. I want at the end to ask the user if he/she want to repeat the program again or to exit the program! for example if he/she press y the program will reask the user to input two number, otherwise the program will be closed. How to do it ?

在这个非常基本的程序中,它要求用户输入两个数字,然后程序会将这些数字相加。我想在最后询问用户他/她是否想再次重复该程序或退出该程序!例如,如果他/她按 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);
}

回答by Tanveer Shaikh

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');
    }

OR you can also try this:

或者你也可以试试这个:

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');
    }

回答by Ashish Ahuja

int main(void) {
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\n",sum);
printf ("Do you want to repeat the operation Y/N: ");
scanf (" %c", &ch);
}
while (ch == 'y' || ch == 'Y');
}

This uses a do-whileloop. It will continue till the condition in the whileof the do-whilewill return false.

这使用了一个do-while循环。这将持续到病情的whiledo-while将返回false。

In simple words, whenever the user enters yor Y, the whileloop will return true. Thus, it will continue.

简单来说,每当用户输入yor 时Ywhile循环都会返回 true。因此,它将继续。

Check thisexample and tutorial for do-whileloop.

检查示例和教程以了解do-while循环。

回答by Hayshin Sabit

[In this program I used 'goto' statement because if I use do while loop then if I enter anything without "Y or y" then the program will be close.To avoid this problem I use 'goto' statement.1

[在这个程序中,我使用了“goto”语句,因为如果我使用 do while 循环,那么如果我输入任何没有“Y 或 y”的内容,那么程序将关闭。为了避免这个问题,我使用了“goto”语句。1

#include<stdio.h>
int main(){
int x, y, sum;
char ch;
print:
    printf ("Enter the first number:");
    scanf ("%d",&x);
    printf ("Enter the second number:");
    scanf ("%d",&y);
    sum=x+y;
    printf ("\nThe total number is:%d\n",sum);
again:
    printf ("\n\t\t\t\t\tDo you want to repeat the operation(Y/N): ");
    scanf (" %c", &ch);

    if(ch == 'y' || ch == 'Y'){
        goto print;
    }
    else if(ch == 'n' || ch == 'N'){
        return 0;
    }
    else{
        printf("\n\t\t\t\t\tPlease enter Yes or NO.\n");
        goto again;
    }
   return 0;

}

}