2017-02-10 24 views
0

爲作業做一個更改制作程序,它必須在輸入金額時返回更改金額(它基於澳大利亞貨幣),並且我已將其工作到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

如果輸入量< 0.05的應用程序會卡住或者您收到金額<更改結果後爲0.05。

原因是這裏:如果您的更改變量值> 0,但是< 0.05您將永久卡住。

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

對於新手來說,這可能很難找到。你的代碼的問題是你使用了錯誤的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