gpt4 book ai didi

无法弄清楚为什么我的代码不起作用

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

几天来我一直在试图找出我的代码出了什么问题;我好像找不到啊如果陈述让我很困惑,所以我想我搞砸了,但仍然不确定。我所知道的是,我非常困惑,并且希望得到我能得到的所有帮助。

#include <stdio.h>

// function main begins program execution
int main(void)
{
int numberOfDays = 0;
float numberOfMiles = 0;
float milesCharge = 0;
float milesTotal = 0;
float total = 0;
float subtotal = 0;
float tax = 0;

do {
printf("%s", "How many days was car rented?\t");
scanf("%d", &numberOfDays);
} while (numberOfDays < 1 );

do {
printf("%s", "How many miles were driven?\t");
scanf("%d", &numberOfMiles);
} while (numberOfMiles > 1);

if (numberOfMiles > 1 || numberOfMiles < 200) {
milesTotal = numberOfMiles * .40;
} else {
milesTotal = numberOfMiles * .35;
}

subtotal = milesTotal + numberOfDays * 15;
tax = subtotal * .06;
total = tax + subtotal;

printf("\nSubtotal:\t\t\t$%.2f\n", subtotal);
printf("Tax Amount:\t\t\t$%.2f\n", tax);
printf("Total:\t\t\t\t$%.2f\n", total);
printf("\n");
}

最佳答案

如果 numberOfMiles 的类型为 float,则必须交换以下行

scanf("%d", &numberOfMiles);

scanf("%f", &numberOfMiles);

或者您可以将类型设置为int

--

如果你想避免无限循环,请交换

} while (numberOfMiles > 1);

} while (numberOfMiles < 1);

顺便说一句。为什么不允许距离短于一英里?

例如通过

} while (numberOfMiles < 0);

要获得更具体的答案,您必须更加精确。

关于无法弄清楚为什么我的代码不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40270171/

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