C 菜单 - 否则

标签 c

我是 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/

相关文章:

c - 为什么在记事本中进行击键时出现异常?

有人可以解释一下这个 "kth smallest integer in an unsorted array"代码吗? - C

python - 导入 C 函数并重新分配变量

c++ - 在 C/C++ 中声明这些类型的表的简洁方法?

c - linux命名fifo非阻塞读select返回bogus read_fds

c - 弹出堆栈的最后一个元素后程序崩溃

c - 找到数组中的最大数

c++ - USB 大容量存储 linux

c - 如何在 C/C++ 中以编程方式创建计划任务?

c - 我应该在项目开始时考虑 GUI 吗?