我是 C 新手,我需要为项目构建一个带有循环的菜单。 我有两个问题。
1) 我想在 else 中添加一个字符,如果在主菜单中按“2”后询问某事,问题将是“你要去参加事件吗?”用户可以输入聊天“Y”或“N”,之后程序将感谢用户并中断,其他输入会将用户发送到主菜单
2)我想计算当我在主菜单上按“3”时程序进行的循环次数,我不知道如何将计数选项与“else if”结合起来
int choice;
while (1)
{
printf("Main Menu\n");
printf("\n");
printf("Please enter num:");
scanf("%d", &choice);
printf("You entered %d\n", choice);
if (choice == 4)
{
break;
}
else if (choice == 1)
printf("Returned to main menu\n");
else if (choice == 2)
printf("Are you going to the event?\n");
else if (choice == 3)
printf("number of loops that made are \n");
else if (choice == 4)
printf("Bye!\n");
else
printf("Wrong Input!\n");
}
printf("Bye!\n");
return 0;
}
最佳答案
对于计数循环问题,只需创建一个 int 变量,并在每个循环结束时向其添加 1。 例如
int count=0;
while(...)
{
*code*
count++;
}
对于您可以使用的其他问题
else if(choice==2)
{
char going;
printf("Are you going to the event? Y/N");
scanf("%c",&going);
if(going=='N'||going=='n')
*code*
system("PAUSE");
return 0;
else if(going=='Y'||going=='y')
{
*code*
}
}
关于C 菜单 - 否则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40847114/