gpt4 book ai didi

c - 开关功能和走出开关的功能

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

我收到了这个问题作为作业,但我还没有走得太远,我不知道如何正确使用 switch 函数,而且我不知道如何来完成它。有人可以帮忙吗?

enter image description here

struct car
{
char model[50];
int manufacture_year;
float price;
};

int main()
{

int i;
int function;
struct car array[2];


for(i=0; i<2; i++) {

printf("what is the cars model? ");
scanf(" %s", &array[i].model);

printf("What year was the car manufactured? ");
scanf(" %d", &array[i].manufacture_year);

printf("How much does it cost? ");
scanf(" %f", &array[i].price);

printf("\n");

}
printf("press 1 to show model, 2 to show price and 3 to terminate");
scanf("%d", &function);


}

这就是我到目前为止所拥有的......我想之后应该会进行切换。

最佳答案

switch 放在输入 switch 变量值的函数(在本例中为 scanf)之后,如下所示:

/* preceding code */
printf("press 1 to show model, 2 to show price and 3 to terminate");
scanf("%d", &function);
switch (function) {
case 1:
show_model(array, 2); /* placeholder */
break;
case 2:
show_price(array, 2); /* placeholder */
break;
case 3:
break;
}

开关的语法如上所示。您要测试的值位于 case 关键字之后。并且,在每种情况的语句之后,通常有一个 break 语句来退出开关。例如,如果第一个 printf 下没有break语句,则执行将继续到下一个语句,这可能是也可能不是。

关于c - 开关功能和走出开关的功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48608347/

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