c - 有没有一种简单的方法可以让用户在每次选择后返回到菜单?

标签 c loops

我的程序应该要求用户做出选择并在返回菜单之前运行该选择。我想我需要break语句,但是我不需要改变所有的if语句来做到这一点吗?现在它会立即遍历所有选择而不会停止。是否有一个简单的解决方案,或者我应该返回并重写每个语句?这是我到目前为止所拥有的:

#include <stdio.h>
#include <math.h>


int main()

{

int choice;
int i, j, k, l;
int num, num2, num3, num4;
int count = 0;

    printf("\t Menu \n");
    printf("1. Multiplication Table \n");
    printf("2. Even or Odd \n");
    printf("3. Number of Digits \n");
    printf("4. Triangle \n");
    printf("5. Exit \n");

    printf("Please choose a menu selection: \n");
    scanf("%d", &choice);

    if(choice = 1);
    {
            for(i = 1; i <= 12; i++)
            {
                    num = i;

            for(j = 1; j<=12; j++)
            {
                    printf("%d\t", (i*j));
            }

            printf("\n");
            }
    }

    if(choice = 2);
    {
            printf("Please enter a whole number: \n");
            scanf("%d", &num2);

            if(num2 % 2 == 0)
                    printf("%d is even. \n", num2);
            else
                    printf("%d is odd. \n", num2);
    }

    if(choice = 3);
    {
            printf("Enter a number: \n");
            scanf("%d", &num3);
    while(num3)
    {
            num3 = num3/10;
            count++;
    }
    printf("The total number of digits in the number is: %d \n", count);
    }

    if(choice = 4);
    {
            printf("Please enter a number for the height: \n");
            scanf("%d", &num4);

            for(k = 1; k <= num4; k++)
            {
            for(l = 1; l <= k; l++)
                    printf("# ");

            printf("\n");
            }
    }

    if(choice = 5)
    {
            printf("Thank you, you will now exit. \n");
    }
    else
    {
            printf("error \n");
    }

return 0;

}

最佳答案

#include <math.h>


int main()

{

int choice;
int i, j, k, l;
int num, num2, num3, num4;
int count = 0;

    do{
    printf("\t Menu \n");
    printf("1. Multiplication Table \n");
    printf("2. Even or Odd \n");
    printf("3. Number of Digits \n");
    printf("4. Triangle \n");
    printf("5. Exit \n");

    printf("Please choose a menu selection: \n");
    scanf("%d", &choice);

    switch(choice){

    case 1:
    {
            for(i = 1; i <= 12; i++)
            {
                    num = i;

            for(j = 1; j<=12; j++)
            {
                    printf("%d\t", (i*j));
            }

            printf("\n");
            }
    } break;

     case 2:
    {
            printf("Please enter a whole number: \n");
            scanf("%d", &num2);

            if(num2 % 2 == 0)
                    printf("%d is even. \n", num2);
            else
                    printf("%d is odd. \n", num2);
    }break;

   case 3:
    {
            printf("Enter a number: \n");
            scanf("%d", &num3);
    while(num3)
    {
            num3 = num3/10;
            count++;
    }
    printf("The total number of digits in the number is: %d \n", count);
    }break;

    case 4:
    {
            printf("Please enter a number for the height: \n");
            scanf("%d", &num4);

            for(k = 1; k <= num4; k++)
            {
            for(l = 1; l <= k; l++)
                    printf("# ");

            printf("\n");
            }
    }break;

    case 5:
    {
            printf("Thank you, you will now exit. \n");
    }break;
    }

 } while(choice < 5 && choice > 0);

    if(choice>5 || choice<=0)
    printf("error \n");


    return 0;

}

关于c - 有没有一种简单的方法可以让用户在每次选择后返回到菜单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58490463/

相关文章:

C 编程 - 二维数组求和

c - 从 GDB 解码 ARM 指令

powershell - 如何在Powershell中添加最大循环?

java - 使用 BufferedWriter 和 Loop 保存 2D JTextField 数组中的文本

iphone - 我可以重新编码循环 UIImageView 以获得更短的代码吗?

c - 如何循环并打印 c 中的结构数组?

编译一个简单的C lua5.0程序,未定义的引用

c - 在管道之后恢复标准输入

java - 关于 Scanner.nextInt() 的无限 while 循环的困惑;

c# - 从 C 插件向 Unity3D 发送字节数据的任何正确方法?