计算时输出不正确

huangapple go评论62阅读模式
英文:

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 &lt;stdio.h&gt;


int
main ()
{
  float fees;
  float projcost;
  float num;

  printf (&quot;Please enter the cost of your project\n&quot;);
  scanf (&quot;%f&quot;, &amp;projcost);

  if (projcost &gt;= 10000)
{
    num = 0.10 * projcost;
    fees = num;
}
  else if (projcost &gt;= 10001 || projcost &lt;= 100000)
{
    num = 1000 + ((projcost - 10000) * 0.05);
  fees = num;
}  
    else (projcost &gt;= 100001);
{    
    num = 6000 + ((projcost - 100000) * 0.03);
    fees = num;
}
    printf (&quot;\nYour fees have totaled to %.2f&quot;, 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

以下是翻译好的部分:

  1. 第一个条件应该被反转,否则后面的条件将不会被考虑:
if (projcost <= 10000)
  1. 第二个条件应该使用逻辑与而不是逻辑或,否则它将始终为真:
elif (projcost >= 10001 and projcost <= 100000)
  1. 在最后一个分支:
else (projcost >= 100001);

你漏掉了一个 if 并且分号放得不对。请修复成:

elif (projcost >= 100001)

然而,由于这是最后一个分支,最好直接移除条件:

else
  1. 因为你使用了浮点数,你列出的条件实际上并不覆盖所有可能的输入。例如,如果输入是 10000.5,那么你的逻辑将会执行一些你没预料到的操作(具体取决于你应用了上述哪种修复方案)。编写这个逻辑的一个更简单的方法是:
if (projcost <= 10000)
    fees = ...;
elif (projcost <= 100000)
    fees = ...;
else
    fees = ...;

这是与你的代码相反的正确做法,因为始终只会执行一个分支。

英文:

There are a few problems with your code.

  1. The first condition should be inverted, otherwise the later ones won't be taken:

     if (projcost &lt;= 10000)
    
  2. The second condition should have a logical and instead of a logical or, otherwise it will always be true:

     else if (projcost &gt;= 10001 &amp;&amp; projcost &lt;= 100000)
    
  3. On the last branch:

     else (projcost &gt;= 100001);
    

    You're missing an if and have an incorrectly placed semicolon. Fix that:

     else if (projcost &gt;= 100001)
    

    However, since it's the last branch you're better to simply remove the condition:

     else
    
  4. 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 &lt;= 10000)
         fees = ...;
     else if (projcost &lt;= 100000)
         fees = ...;
     else
         fees = ...;
    

    Which is, contrary to your code, is correct -- because exactly one branch will be taken, always.

huangapple
  • 本文由 发表于 2023年2月27日 10:39:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75576357.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定