C编程循环开关情况(如果为默认情况)
我正在对布尔表达式使用switch case,其答案为yes或no。但如果答案无效,我希望它能够返回。我如何使用if或while循环,其答案在字符中可以变化为“y”“y”“n”“n”?下面的if语句是否有效C编程循环开关情况(如果为默认情况),c,loops,if-statement,switch-statement,else,switch,for,C,Loops,If Statement,Switch Statement,Else,Switch,For,我正在对布尔表达式使用switch case,其答案为yes或no。但如果答案无效,我希望它能够返回。我如何使用if或while循环,其答案在字符中可以变化为“y”“y”“n”“n”?下面的if语句是否有效 if (mstatus != 'y', 'Y', 'N', 'n') { switch(mstatus) case 'y': printf("You have answer yes for married"); brea
if (mstatus != 'y', 'Y', 'N', 'n')
{
switch(mstatus)
case 'y':
printf("You have answer yes for married");
break;
case 'Y':
printf("You have answer yes for married");
break;
case 'n':
printf("You have answer no for married");
break;
case 'N':
printf("You have answer no for married");
break;
}
else
{
default:
printf("please re-enter a valid answer");
scanf(" %c", &mstatus);
}
return 0;
你根本不需要在那里用
if..else
块
如果mstatus
没有y',y',N',N'
中的任何一个,控件将转到默认情况
但是,您可以将switch
语句块放入while(1)
/while(someFlag)
循环中,并在想要中断循环时(取消)设置someFlag
。switch
语句的一般格式如下:
switch(expression) {
case constant-expression :
statement(s);
break; /* optional */
case constant-expression :
statement(s);
break;
.
.
.
default :
statement(s);
break;
这意味着如果没有匹配的案例,将选择默认的。因此,您不需要在and语句中包含default
大小写。你可以进一步了解一下
如果要连续读取字符直到匹配其中一种情况,请在循环中包含开关
,如下所示:
int flag = 0;
for (; flag == 1;)
{
switch(mstatus)
{
case 'y':
case 'Y':
printf("You have answer yes for married");
flag = 1;
break;
case 'n':
case 'N':
printf("You have answer no for married");
flag = 1;
break;
default:
printf("please re-enter a valid answer");
scanf(" %c", &mstatus);
}
}
您的if
语句不起作用,因为您无法将变量与一系列值进行比较。另外,switch
语句可能有些过分。如果您只是想在得到正确答案之前进行循环,那么您可以这样做:
for(;;)
{
char mstatus;
printf("please enter a marriage status");
scanf(" %c", &mstatus);
if(mstatus == 'y' || mstatus =='Y')
{
printf("You have answer yes for married");
break;
}
else if(mstatus == 'n' || mstatus =='N')
{
printf("You have answer no for married");
break;
}
else
{
printf("please re-enter a valid answer");
}
}
在您提供的代码中,您根本没有添加循环。您需要添加一个循环才能使其工作
此外,正如Saurav所提到的,不需要在switch语句之上使用if、then-else语句
有很多方法可以做到这一点。一种方法是:
do
{
printf("Answer (y/n)? :");
scanf(" %c", &mstatus);
repeat = 0;
switch(mstatus)
{
case 'y':
case 'Y':
printf("You have answer yes for married");
break;
case 'n':
case 'N':
printf("You have answer no for married");
break;
default:
printf("please re-enter a valid answer");
repeat = 1;
}
} while (repeat == 1);
将逻辑封装在函数中,这样,当您想要退出循环时,可以从函数返回:
#include <stdio.h>
int yesno(const char *q)
{
char answer = '\0';
while (1) {
printf("%s? (y/n) ", q);
fflush(stdout);
scanf(" %c", &answer);
switch (answer) {
case 'N':
case 'n':
return 0;
case 'Y':
case 'y':
return 1;
default:
puts("Please answer y or n");
}
}
/* unreachable */
return -1;
}
int main(void)
{
int yes;
yes = yesno("Are you married");
printf("You answered %s\n", yes ? "yes" : "no");
return 0;
}
#包括
int yesno(常量字符*q)
{
字符答案='\0';
而(1){
printf(“%s?(y/n)”,q);
fflush(stdout);
scanf(“%c”和“应答”);
开关(应答){
案例“N”:
案例“n”:
返回0;
案例“Y”:
案例“y”:
返回1;
违约:
puts(“请回答y或n”);
}
}
/*遥不可及*/
返回-1;
}
内部主(空)
{
int是;
你结婚了吗;
printf(“您回答了%s\n,是吗?“是”:“否”);
返回0;
}
请回答问题并正确缩进代码。如果有,请提供一个直通式开关案例逻辑的案例(双关语)。谢谢,您的回答为我提供了另一个关于编码的见解。