用c语言编写狡猾的if语句?
我正在编写代码,读取用水输入并显示账单。固定价格是10美元。前30米每米0.6美元,后20米每米0.8美元,后10米每米1.2美元,其他米为1.2美元,但出于某种原因,我的代码要求用户输入一个值,而不做任何其他操作。我做错了什么?是我的if语句吗?我觉得他们还可以,我的计算可能是个问题。以下是迄今为止的代码:用c语言编写狡猾的if语句?,c,if-statement,multiplication,C,If Statement,Multiplication,我正在编写代码,读取用水输入并显示账单。固定价格是10美元。前30米每米0.6美元,后20米每米0.8美元,后10米每米1.2美元,其他米为1.2美元,但出于某种原因,我的代码要求用户输入一个值,而不做任何其他操作。我做错了什么?是我的if语句吗?我觉得他们还可以,我的计算可能是个问题。以下是迄今为止的代码: #include <stdio.h> int main() { int watercom, fixedrate, first30, next20, next10, ad
#include <stdio.h>
int main()
{
int watercom, fixedrate, first30, next20, next10, additional, firstcal, secondcal,thirdcal, fourthcal;
printf("what is our water consumption?\n");
scanf("%i\n",&watercom);
fixedrate = 10;
first30 = 0.6;
next20 = 0.8;
next10 = 1.0;
additional = 1.2;
firstcal = fixedrate * first30;
secondcal = fixedrate * next20;
thirdcal = fixedrate * next10;
fourthcal = fixedrate * additional;
if ( watercom <= 30)
printf("your bill is %i", &firstcal);
else if (watercom >= 31 && watercom <= 50)
printf("your bil is %i", &secondcal);
else if (watercom >= 51 && watercom >= 60)
printf("your bill is %i", &thirdcal);
else if (watercom >= 61)
printf("your water bill is %i", fourthcal);
return 0;
}
#包括
int main()
{
int watercom、fixedrate、first30、next20、next10、additional、firstcal、secondcal、thirdcal、fourthcal;
printf(“我们的用水量是多少?\n”);
scanf(“%i\n”、&watercom);
固定速率=10;
前30位=0.6;
next20=0.8;
next10=1.0;
附加=1.2;
firstcal=固定利率*first30;
secondcal=固定利率*next20;
第三方=固定方*next10;
四次方=固定方*附加方;
如果(watercom=31&&watercom=51&&watercom>=60)
printf(“您的账单是%i”、&thirdcal);
否则如果(watercom>=61)
printf(“您的水费账单是%i”,第四卡);
返回0;
}
在我看来,这篇文章的第一版中出现的计算代数似乎完全错了。为了让你开始:除了这些amount*rate
乘法之外,你还需要执行一个加法---一个固定的利率期限,如果你超过了最初的津贴,再加上另一个金额,当你进入下一个价格区间时再加上另一个金额,依此类推。要计算这些条款中的每一项,您需要在继续计算时减去先前条款中已支付的水量
但这不是问题所在。你问为什么程序根本不打印任何东西。对我来说,在Windows上使用VisualStudio制作了一个命令行可执行文件之后,我复制了这个问题:我输入了值,然后似乎什么也没发生。但是当我按下control-C停止程序时,的答案就被打印出来了
要解决此问题,请从scanf
命令中删除\n
然后继续你的其他问题,包括(a)正确的代数,和(b)打印值(例如
firstCal
)而不是变量的地址(&firstCal
)。除了其他人指出的以外,你所有的变量都是int
,但有些必须是双精度的[因为您正在将它们设置为0.6
,等等]:
int watercom;
double fixedrate;
double first30;
double next20;
double next10;
double additional;
int firstcal;
int secondcal;
int thirdcal;
int fourthcal;
是的,你唯一的int是fixedRate,其余的是float。
同样根据你在问题中所说的,这不是你程序中的内容。如果fixedRate是10,前30个的速率是.6,那么如果我输入30,我应该得到fixedRate+(30*.6)。
您需要将fixedRate添加到所有计算中,并将每一层的计算一起添加到最终金额中
在编写程序之前,先把问题写在纸上。在测试程序之前,你应该知道预期的输出。
如果watercom>=60
是watercom Infirst30=0.6;
您正在为int
分配一个double
值,其值将被截断为0
。坚持使用int
,但以美分计算。first30=60;
。为什么不使用float
或double
?答案很多请不要用double
或float
来赚钱,用美分计算。如果你用整数乘以一个浮点,你不会得到一个浮点吗?那么所有的*cal变量也应该是浮点吗?我不知道“美分”是一个有效的C原语。@WeatherVane OP的代码将int设置为0.6,等等。只是纠正了这一点,而不是使用0.6是否合适not@ArchonOSX这不是我为什么不使用code
格式的原因。谢谢这简化了我的代码和“&”运算符是一个坏习惯!谢谢。我现在从scanf和printf语句中删除了\n运算符以修复代数!祝你好运。我想你也会像其他人建议的那样从使用int
切换到float
或double
。这意味着当你打印结果时,你会我们需要一个不同的格式说明符:%f
或%g
用于float
s(%lf
或%lg
用于double
s)。
if ( watercom <= 30 )
printf("your bill is %i", firstcal);
else if ( watercom <= 50)
printf("your bill is %i", secondcal);
else if ( watercom <= 60)
printf("your bill is %i", thirdcal);
else
printf("your water bill is %i", fourthcal);
int watercom;
double fixedrate;
double first30;
double next20;
double next10;
double additional;
int firstcal;
int secondcal;
int thirdcal;
int fourthcal;