gpt4 book ai didi

c - 创建 if else 程序来计算不同车辆的不同 parking 费用时遇到麻烦

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

我无法使此代码正常工作。我刚刚学习了这些内容(if else 语句),并且必须通过创建一个计算每辆不同车辆的 parking 费用的程序来将其付诸实践。 c 代表汽车(每小时 2 美元),b 代表巴士(每小时 3 美元),t 代表卡车(每小时 4 美元)。这是使用 dev-c++ 编译器进行的 C 编程。

感谢所有反馈,提前谢谢您!

#include <stdio.h>

//declaration
char parkingCharge (int pc);
int pc;
int h, total, c, b, t;
char v;

int main (void)
{
//statements

printf ("Enter type of vehicle (c for car, ");
printf ("b for bus, or t for truck): ");
scanf ("%c", &v);

printf ("How long did you park: ");
scanf ("%d", &h);

total = pc;
printf ("Your total is: %d", total);

return 0;
}

char parkingCharge (int pc)
{
//statements

if (v == c){
pc = 2 * h;
}
else if (v == b){
pc = 3 * h;
}
else if (v == t){
pc = 4 * h;
}
return total;
}

最佳答案

#include <stdio.h>

//declaration
int parkingCharge (char v, int h);
int h;
char v;

int main (void)
{
//statements

printf ("Enter type of vehicle (c for car, ");
printf ("b for bus, or t for truck): ");
scanf ("%c", &v);

printf ("How long did you park: ");
scanf ("%d", &h);
// You forgot to call the function parkingCharge.
int total = parkingCharge(v, h);
printf ("Your total is: %d\n", total);

return 0;
}
// parkingCharge should return an int value and you could pass
// v and h as parameters.
int parkingCharge (char v, int h)
{
int pc;
// Remember when you compare a variable with a constant char, you
// put the constant char value between ''.
if (v == 'c'){
pc = 2 * h;
}
else if (v == 'b'){
pc = 3 * h;
}
else if (v == 't'){
pc = 4 * h;
}
return pc;
}

关于c - 创建 if else 程序来计算不同车辆的不同 parking 费用时遇到麻烦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29198086/

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