gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 16:11:49 26 4
gpt4 key购买 nike

我的程序应该要求用户做出选择并在返回菜单之前运行该选择。我想我需要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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com