C#更改制作程序

问题描述:

为作业做一个更改制作程序,它必须在输入金额时返回更改金额(它基于澳大利亚货币),并且我已将其工作到50美分。当计算与变化,程序必须返回一个二十美分,十美分的值或五分变化,程序冻结C#更改制作程序

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void btnCalculate_Click(object sender, EventArgs e) 
    { 
     double change = Convert.ToDouble(txtOffered.Text) - Convert.ToDouble(txtDue.Text); 
     // MessageBox.Show(change.ToString());    
     double hund = 100; 
     double fifty = 50; 
     double twent = 20; 
     double ten = 10; 
     double five = 5; 
     double two = 2; 
     double one = 1; 
     double fifcent = 0.50; 
     double twentcent = 0.20; 
     double tencent = 0.10; 
     double fivecent = 0.05; 


     while (change > 0) 
     { 
      if (change >= hund) 
      { 
       txtChange.Text += "1x $100 \r\n"; 
       change = change - hund; 
      } 


      else if (change >= fifty) 
      { 
       txtChange.Text += "1x $50 \r\n"; 
       change = change - fifty; 
      } 
      if (change >= twent) 
      { 
       txtChange.Text += "1x $20 \r\n"; 
       change = change - twent; 
      } 
      else if (change >= ten) 
      { 
       txtChange.Text += "1x $10 \r\n"; 
       change = change - ten; 
      } 
      if (change >= five) 
      { 
       txtChange.Text += "1x $5 \r\n"; 
       change = change - five; 
      } 
      else if (change >= two) 
      { 
       txtChange.Text += "1x $2 \r\n"; 
       change = change - two; 
      } 
      if (change >= one) 
      { 
       txtChange.Text += "1x $1 \r\n"; 
       change = change - one; 
      } 
      else if (change >= fifcent) 
      { 
       txtChange.Text += "1x 50c \r\n"; 
       change = change - fifcent; 
      } 
      if (change >= twentcent) 
      { 
       txtChange.Text += "1x 20c \r\n"; 
       change = change - twentcent; 
      } 
      else if (change >= tencent) 
      { 
       txtChange.Text += "1x 10c \r\n"; 
       change = change - tencent; 
      } 
      if (change >= fivecent) 
      { 
       txtChange.Text += "1x 5c \r\n"; 
       change = change - fivecent; 
      } 

     } 

    } 
} 
+2

那么这里是你的时间来学习如何调试,跟踪应用程序,看看它卡住的地方! – BugFinder

+0

这应该不会导致应用程序冻结,但是您应该可以在任何地方使用“else if”,除了第一个“if” – dlxeon

如果输入量< 0.05的应用程序会卡住或者您收到金额<更改结果后为0.05。

原因是这里:如果您的更改变量值> 0,但是< 0.05您将永久卡住。

while (change > 0) 
{ 
    ... 
    if (change >= fivecent) 
    { 
     txtChange.Text += "1x 5c \r\n"; 
     change = change - fivecent; 
    } 
} 

对于新手来说,这可能很难找到。你的代码的问题是你使用了错误的DataType。取而代之的double,你应该用decimal

decimal change = Convert.ToDouble(txtOffered.Text) - Convert.ToDouble(txtDue.Text); 
// MessageBox.Show(change.ToString());    
decimal hund = 100; 
decimal fifty = 50; 
decimal twent = 20; 
decimal ten = 10; 
decimal five = 5; 
decimal two = 2; 
decimal one = 1; 
decimal fifcent = 0.50m; 
decimal twentcent = 0.20m; 
decimal tencent = 0.10m; 
decimal fivecent = 0.05m; 

有了这个,你的代码也不会冻结。

对此的解释,问题是,使用double0.25 - 0.20的结果是... 0.049999999999999989。这是因为double使用浮点值,这可能导致舍入问题。如果您想了解更多关于浮点计算的信息,请查看here