是或否以退出switch语句
是否可以创建yes或no函数来调用以退出是或否以退出switch语句,c,C,是否可以创建yes或no函数来调用以退出开关语句。如果(y)es被击中,它将退出。如果(n)o被点击,它将返回到交换机的选项。如果是这样的话,将如何做到这一点。这就是我目前所拥有的 我希望这有助于澄清我想做什么 int yes_no(void) { scanf("%d", &option); while (option == 'y' || option == 'Y' || option == 'n' || option == 'N') { if (op
开关
语句。如果(y)es被击中,它将退出。如果(n)o被点击,它将返回到交换机的选项。如果是这样的话,将如何做到这一点。这就是我目前所拥有的
我希望这有助于澄清我想做什么
int yes_no(void) {
scanf("%d", &option);
while (option == 'y' || option == 'Y' || option == 'n' || option == 'N') {
if (option == 'y' || option == 'Y') {
return 1;
} else
if (option == 'n' || option == 'N') {
return 0;
} else {
printf("Only (Y)es or (N)o are acceptable");
}
}
}
int main(void) {
int select;
do {
printf("0 exit");
printf("1 hello");
scanf("%d", &select);
switch (select) {
case 0:
printf("Exit the program? (Y)es or (N)o :");
yes_no(); // if (n)o is hit it will loop back to the menu
break;
case 1:
printf("hello how r u doing");
break;
default:
printf("not accepted number");
}
} while (select != 0);
}
如果我没有弄错你的问题,这应该可以解决问题:
int yes_or_no()
{
do
{
char option = 0;
scanf(" %c", &option);
if (option == 'y' || option == 'Y')
return 1;
if (option == 'n' || option == 'N')
return 0;
printf("Only (Y)es or (N)o are acceptable");
}
while( 1 );
}
如果我没有弄错你的问题,这应该可以解决问题:
int yes_or_no()
{
do
{
char option = 0;
scanf(" %c", &option);
if (option == 'y' || option == 'Y')
return 1;
if (option == 'n' || option == 'N')
return 0;
printf("Only (Y)es or (N)o are acceptable");
}
while( 1 );
}
考虑以下
yes\u no
功能
int yes_no() {
char option;
while (1) {
printf("(y/n): ");
if (scanf(" %c", &option) != 1) {
printf("Error occurred while reading option.\n");
continue;
}
if (option == 'y' || option == 'Y') {
return 1;
} else if (option == 'n' || option == 'N') {
return 0;
} else {
printf("Only (y)es or (n)o are acceptable.\n");
}
}
}
如果输入yes
,则返回值为1
;如果输入no
,则返回值为0
。记住这一点,在switch
语句的情况0
代码块中,可以捕获此函数的返回值,然后可以使用该值中断循环或不执行任何操作。例如:
int main(void) {
int select;
int continue_loop = 1;
printf("Press 0 to exit\n");
printf("Press 1 for hello\n\n");
while (continue_loop) {
printf("(0/1): ");
if (scanf(" %d", &select) != 1) {
printf("Error occurred while reading number.\n");
continue;
}
switch (select) {
case 0:
printf("Exit the program? (y)es or (n)o;\n");
int make_sure = yes_no(); // yes_no() returns 1 if 'yes', and 0 if 'no'
// If 'yes', break while loop by setting continue_loop to 0.
// Do nothing in case of 'no'
if (make_sure == 1) {
continue_loop = 0;
}
break;
case 1:
printf("Hello, how are you doing?\n");
break;
default:
printf("Number not accepted.\n");
}
}
return 0;
}
考虑以下
yes\u no
功能
int yes_no() {
char option;
while (1) {
printf("(y/n): ");
if (scanf(" %c", &option) != 1) {
printf("Error occurred while reading option.\n");
continue;
}
if (option == 'y' || option == 'Y') {
return 1;
} else if (option == 'n' || option == 'N') {
return 0;
} else {
printf("Only (y)es or (n)o are acceptable.\n");
}
}
}
如果输入yes
,则返回值为1
;如果输入no
,则返回值为0
。记住这一点,在switch
语句的情况0
代码块中,可以捕获此函数的返回值,然后可以使用该值中断循环或不执行任何操作。例如:
int main(void) {
int select;
int continue_loop = 1;
printf("Press 0 to exit\n");
printf("Press 1 for hello\n\n");
while (continue_loop) {
printf("(0/1): ");
if (scanf(" %d", &select) != 1) {
printf("Error occurred while reading number.\n");
continue;
}
switch (select) {
case 0:
printf("Exit the program? (y)es or (n)o;\n");
int make_sure = yes_no(); // yes_no() returns 1 if 'yes', and 0 if 'no'
// If 'yes', break while loop by setting continue_loop to 0.
// Do nothing in case of 'no'
if (make_sure == 1) {
continue_loop = 0;
}
break;
case 1:
printf("Hello, how are you doing?\n");
break;
default:
printf("Number not accepted.\n");
}
}
return 0;
}
使用switch,您可以更好地执行代码:
int yes_no(void) {
scanf("%d", &option);
while (1){
switch(option){
case 'y':
case 'Y':
return 1;
case 'n':
case 'N':
return 0;
default:
printf("invalid input.try again");
scanf("%d", &option);
}
}
}
使用switch,您可以更好地执行代码:
int yes_no(void) {
scanf("%d", &option);
while (1){
switch(option){
case 'y':
case 'Y':
return 1;
case 'n':
case 'N':
return 0;
default:
printf("invalid input.try again");
scanf("%d", &option);
}
}
}
返回0代码>->继续代码>您的代码中没有switch语句,您应该使用switch语句。我确信您想要完成的事情可以很容易地完成,但不一定是您描述的方式(我认为…它不是很清楚)。你到底想让你的程序做什么?循环,执行用户选择的操作,直到用户说停止为止。调用任何scanf()
函数族时,始终检查返回值(而不是参数值),以确保操作成功。返回0代码>->继续代码>您的代码中没有switch语句,您应该使用switch语句。我确信您想要完成的事情可以很容易地完成,但不一定是您描述的方式(我认为…它不是很清楚)。你到底想让你的程序做什么?循环,执行用户选择的操作,直到用户说要停止?调用任何scanf()
函数族时,始终检查返回值(而不是参数值)以确保操作成功。我认为您应该首先测试代码。如果您输入了在if
语句中未处理的内容,会发生什么?哦,好吧,这就是为什么否决票。。。当然,它不是防弹的,但正如您现在可能看到的,有效地处理用户输入并不是一项简单的任务。但我不认为这是OP的要求,他似乎只是要求一些基本的东西。但是,请继续,发布一些防弹的用户输入处理,我将向上投票。要么删除while
循环,要么通过修复标识符来防止无限循环。还有,别那样。我只是想让你改进你的答案,没有别的。或者,如果你不想发布答案,也许你可以编辑我的答案并修正它来改进它?继续。@IharobAlAsimi只有当scanf()
失败(比如在EOF上)或从未输入有效输入时,此循环才会无限。但是,它会为每个无效字符(包括空格)打印消息。我认为您应该首先测试代码。如果您输入了在if
语句中未处理的内容,会发生什么?哦,好吧,这就是为什么否决票。。。当然,它不是防弹的,但正如您现在可能看到的,有效地处理用户输入并不是一项简单的任务。但我不认为这是OP的要求,他似乎只是要求一些基本的东西。但是,请继续,发布一些防弹的用户输入处理,我将向上投票。要么删除while
循环,要么通过修复标识符来防止无限循环。还有,别那样。我只是想让你改进你的答案,没有别的。或者,如果你不想发布答案,也许你可以编辑我的答案并修正它来改进它?继续。@IharobAlAsimi只有当scanf()
失败(比如在EOF上)或从未输入有效输入时,此循环才会无限。不过,它会为每个无效字符(包括空格)打印消息。