英文:
incorrect output when calculated
问题
我是编程世界的初学者,目前正在学习C语言编程,但似乎无法弄清楚我的代码出了什么问题。
如果我使用小于100,000的数字,我得到的答案完全不同于我自己的计算。我相信我的计算是正确的,所以不明白问题出在哪里。我一直试图输入7000,但输出的是3210,而答案应该是700。有人可以帮忙吗?
#include <stdio.h>
int main()
{
float fees;
float projcost;
float num;
printf("请输入项目的成本\n");
scanf("%f", &projcost);
if (projcost >= 10000)
{
num = 0.10 * projcost;
fees = num;
}
else if (projcost >= 10001 || projcost <= 100000)
{
num = 1000 + ((projcost - 10000) * 0.05);
fees = num;
}
else (projcost >= 100001);
{
num = 6000 + ((projcost - 100000) * 0.03);
fees = num;
}
printf("\n您的费用总计为%.2f", fees);
return 0;
}
如果我使用小于100,000的数字,我得到的答案完全不同于我自己的计算。我相信我的计算是正确的,所以不明白问题出在哪里。我一直试图输入7000,但输出的是3210,而答案应该是700。有人可以帮忙吗?
英文:
So im a beginner to the programming world and i'm currently learning to program in C and i can't seem to figure out what is going wrong with my code.
#include <stdio.h>
int
main ()
{
float fees;
float projcost;
float num;
printf ("Please enter the cost of your project\n");
scanf ("%f", &projcost);
if (projcost >= 10000)
{
num = 0.10 * projcost;
fees = num;
}
else if (projcost >= 10001 || projcost <= 100000)
{
num = 1000 + ((projcost - 10000) * 0.05);
fees = num;
}
else (projcost >= 100001);
{
num = 6000 + ((projcost - 100000) * 0.03);
fees = num;
}
printf ("\nYour fees have totaled to %.2f", fees);
return 0;
}
If i use a number that is lower than 100,000 i get a completely different answer to what i calculate myself. i believe my calculations are correct so i don't understand what is the issue. i have been trying to input 7000 but it outputs 3210 when the answer is supposed to be 700. can anyone help?
答案1
得分: 3
以下是翻译好的部分:
- 第一个条件应该被反转,否则后面的条件将不会被考虑:
if (projcost <= 10000)
- 第二个条件应该使用逻辑与而不是逻辑或,否则它将始终为真:
elif (projcost >= 10001 and projcost <= 100000)
- 在最后一个分支:
else (projcost >= 100001);
你漏掉了一个 if
并且分号放得不对。请修复成:
elif (projcost >= 100001)
然而,由于这是最后一个分支,最好直接移除条件:
else
- 因为你使用了浮点数,你列出的条件实际上并不覆盖所有可能的输入。例如,如果输入是
10000.5
,那么你的逻辑将会执行一些你没预料到的操作(具体取决于你应用了上述哪种修复方案)。编写这个逻辑的一个更简单的方法是:
if (projcost <= 10000)
fees = ...;
elif (projcost <= 100000)
fees = ...;
else
fees = ...;
这是与你的代码相反的正确做法,因为始终只会执行一个分支。
英文:
There are a few problems with your code.
-
The first condition should be inverted, otherwise the later ones won't be taken:
if (projcost <= 10000)
-
The second condition should have a logical and instead of a logical or, otherwise it will always be true:
else if (projcost >= 10001 && projcost <= 100000)
-
On the last branch:
else (projcost >= 100001);
You're missing an
if
and have an incorrectly placed semicolon. Fix that:else if (projcost >= 100001)
However, since it's the last branch you're better to simply remove the condition:
else
-
Because you're using floating point, the conditions that you listed don't actually cover all the possible inputs. For example, if the input is
10000.5
, then your logic will do something that you didn't expect (what exactly depends on which of the above fixes you apply). A simpler way of writing this logic would be:if (projcost <= 10000) fees = ...; else if (projcost <= 100000) fees = ...; else fees = ...;
Which is, contrary to your code, is correct -- because exactly one branch will be taken, always.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论