C程序无法显示输出

时间:2016-11-07 07:33:47

标签: c

我的C程序没有显示输出。

我的C语言代码:

int x = 0; // Installment
int y = 0  // Balance

for (i=1; i<=installment;i++)
        {
            printf("%d %d %d\n", i, x=totalFee/installment, y = totalFee-(totalFee/instalment));
        }

正确输出:

Total fees: 300
Month  Installment  Balance
  1      100          200
  2      100          100
  3      100            0

我的输出:

Total fees: 300
Month  Installment  Balance
  1      100          200
  2      100          200
  3      100          200

这只是代码的一部分。因为这是我遇到问题的部分。其他部分都很好。

4 个答案:

答案 0 :(得分:2)

试试这个:

for (i=1; i<=installment;i++)
{
    x = totalFee/installment;
    y = totalFee-x;
    printf("%d %d %d\n", i, x, y);
}

在C / C ++中,编译器决定在调用函数时评估参数的顺序。绝对不能保证订单从第一个到最后一个参数。因此,很有可能在totalFee-x之前评估x = totalFee/installment,这与您的预期不符。

请参阅Compilers and argument order of evaluation in C++Order of evaluation in C++ function parameters甚至function parameter evaluation order。 特别是,请检查this answer

现在您更新了帖子,并由y = totalFee-x替换为y = totalFee-(totalFee/instalment)。最后一个应该有效y作业不依赖于x。如果它对您不起作用,那只是因为您正在以错误的方式进行操作。使用调试器查看正在进行的操作。

答案 1 :(得分:1)

问题可能与功能参数的评估顺序有关。您无法知道或假设y = totalFee-xx = totalFee/installment; y = totalFee-x; printf("%d %d %d\n", i, x, y); 之前执行。此外,在表达式中使用赋值通常是不好的做法。

尝试将循环体更改为:

<asp:TextBox ID="TextBox2" runat="server" CssClass="form-control"></asp:TextBox> 
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" 
ErrorMessage="ID length is Less than 10" ControlToValidate="TextBox2" ValidationExpression="^[a-zA-Z0-9]{10,1000}$">
</asp:RegularExpressionValidator>
  <br />
  <asp:Button ID="Button1" runat="server" Text="Button" />

答案 2 :(得分:0)

也许你不理解你的循环

让我们一步一步。

首先, i = 1,x = 300/3 = 100,y = 300-100 = 200

其次, i = 2,x = 300/3 = 100,y = 300-100 = 200

第三, i = 2,x = 300/3 = 100,y = 300-100 = 200

x你做到100

答案 3 :(得分:0)

所需输出的程序

for (i=1; i<=installment;i++)
{
    x = totalFee/installment;
    y = totalFee-(x * i);
    printf("%d %d %d\n", i, x, y);
}