gpt4 book ai didi

c - 如何在 C 中获取输入参数并使其返回 float

转载 作者:行者123 更新时间:2023-11-30 21:20:05 25 4
gpt4 key购买 nike

我有一项作业,我必须编写自己的 C 代码,该代码接受一个输入参数并给出一个 float 作为输出。目前我正致力于实现这一目标。我尝试了很多不同的方法来谷歌搜索,但一无所获。

我的代码允许用户输入他们想要微波炉加热的墨西哥卷饼数量,并根据他们的瓦数,为他们提供建议的在微波炉中加热的时间。

这是我现在拥有的。

#include <stdio.h>
int LowWatts(int burritos);
int MediumWatts(int burritos);
int HighWatts(int burritos);
int main ()
{

int burritos, menuSelect;
float Results;
burritos = (double)1;


while (burritos > 0)
{
printf ("Enter the amount of burritos you wish to cook \n: ");
scanf("%d", &burritos);
printf("%d\n", burritos);

if (burritos > 0)
{
printf ("Enter 1 1100W, 2 to 1200W, 3 1250W microwave \n: ");
scanf("%d", &menuSelect);
printf("%d\n", menuSelect);
if (menuSelect == 1)
{
Results = LowWatts(burritos);
printf("For %d burrito(s) %.2f minutes is recomended for "
"1100W microwave\n", burritos, Results);
}
else if (menuSelect == 2)
{
Results = MediumWatts(burritos);
printf("For %d burrito(s) %.2f minutes is recomended for "
"1200W microwave\n", burritos, Results);
}
else if (menuSelect == 3)
{
Results = HighWatts(burritos);
printf("For %d burrito(s) %.2f minutes is recomended for "
"1250W microwave\n", burritos, Results);
}
else
printf("\nInvalid selection, please choose 1, 2, or 3 \n");
}
}
return 0;
}
int LowWatts(int burritos)
{
return (double)(burritos*1.10);
}

int MediumWatts(int burritos)
{
return burritos*1.00;
}

int HighWatts(int burritos)
{
return burritos*0.55;
}

最佳答案

您的函数都设置为 int 返回类型,因此无论您在 中进行何种类型的转换,它们都只会返回 int >返回语句。

将它们更改为返回floatdouble

关于c - 如何在 C 中获取输入参数并使其返回 float ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43669538/

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