2016-10-05 78 views
1

首先我想爲凌亂代碼道歉。目前在大學一年級,有一個問題,我自己或我的朋友都不知道。該問題要求接受來自用戶的美元輸入,並找出多少二十,幾十,五和一個將增加最少數量的賬單。建立解決方案導致沒有錯誤,但一旦運行並且數據被輸入即刻崩潰。我嘗試使用調試器無濟於事,以及更改代碼(添加'if''then'語句等)。我通常是'while'循環的新手,並且一般編碼,所以如果我正在製作愚蠢的錯誤隨時烤我。 先進的謝謝!Windows窗體應用程序崩潰同時循環

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

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

    private void textBox1_TextChanged(object sender, EventArgs e) 
    { 

    } 

    private void btnFindDem_Click(object sender, EventArgs e) 
    { 
     int twenties; 
     int tens; 
     int fives; 
     int singles; 
     int totDollars; 
     int sub1; 
     int sub2; 
     int sub3; 
     int sub4; 
     totDollars = Convert.ToInt32(txtDollars.Text); 
     twenties = 0; 
     tens = 0; 
     fives = 0; 
     singles = 0; 
     sub1 = totDollars; 
     sub2 = totDollars; 
     sub3 = totDollars; 
     sub4 = totDollars; 
     lstOut.Items.Add("Total dollars = " + totDollars.ToString("c")); 
      while (totDollars >= 20) 
       { 
        sub1 = sub1 - 20; 
        twenties = twenties + 1; 
       } 
      while (totDollars >= 10) 
       { 
        sub2 = sub2 - 10; 
        tens = tens + 1; 
       } 
      while (totDollars >= 5) 
       { 
        sub3 = sub3 - 5; 
        fives = fives + 1; 
       } 
      while (totDollars >= 1) 
       { 
        sub4 = sub4 - 1; 
        singles = singles + 1; 
       } 
     lstOut.Items.Add("Twenties: " + twenties); 
     lstOut.Items.Add("Tens: " + tens); 
     lstOut.Items.Add("Fives: " + fives); 
     lstOut.Items.Add("Ones: " + singles); 




     } 
    } 
} 
+0

你是什麼意思的「崩潰」。正在報告什麼異常?使用Visual Studio的調試器運行時會發生什麼? – hatchet

+0

我得到的只是'沒有迴應',它只是坐在那裏,直到我結束這個過程。 –

+0

你爲什麼要從這些'sub'變量中減去而不是'totDollars'?既然你沒有從totDollars中減去,你的第一個循環將是無限的。 – hatchet

回答

1

您沒有調整您在while循環條件中使用的變量的值。這:

sub1 = totDollars; 
sub2 = totDollars; 
sub3 = totDollars; 
sub4 = totDollars; 
lstOut.Items.Add("Total dollars = " + totDollars.ToString("c")); 
    while (totDollars >= 20) 
     { 
      sub1 = sub1 - 20; 
      twenties = twenties + 1; 
     } 

應該

lstOut.Items.Add("Total dollars = " + totDollars.ToString("c")); 
    while (totDollars >= 20) 
     { 
      totDollars = totDollars - 20; 
      twenties = twenties + 1; 
     } 

與其他同時進行的類似變化循環

0

首先確保txtDollars.Text的價值確實是一個整數,否則程序會崩潰;

while循環使用的條件永遠不會改變。所以while循環永遠不會結束。

while (totDollars >= 20) 
{ 
    sub1 = sub1 - 20; 
    twenties = twenties + 1; 
} 

totDollars永遠不會改變,所以它總是會大於20並永不結束。快速解決這個問題是刪除像下面這樣的子變量。

while (totDollars >= 20) 
{ 
    totDollars = totDollars - 20; 
    twenties = twenties + 1; 
} 

我還包括該方法的另一個版本,並進行了一些代碼優化。您會注意到使用 - =,++和 - 運算符。知道這些真的很方便。另外,請注意,您可以通過一些簡潔的方式聲明變量並在一個步驟中設置值。

private void btnFindDem_Click(object sender, EventArgs e) 
{ 
    int twenties = 0, 
     tens = 0, 
     fives = 0, 
     singles = 0, 
     totDollars = Convert.ToInt32(txtDollars.Text); 
    lstOut.Items.Add("Total dollars = " + totDollars.ToString("c")); 
    while (totDollars >= 20) 
    { 
     totDollars -= 20; 
     twenties++; 
    } 
    while (totDollars >= 10) 
    { 
     totDollars -= 10; 
     tens++; 
    } 
    while (totDollars >= 5) 
    { 
     totDollars-= 5; 
     fives++; 
    } 
    while (totDollars >= 1) 
    { 
     totDollars--; 
     singles++; 
    } 
    lstOut.Items.Add("Twenties: " + twenties); 
    lstOut.Items.Add("Tens: " + tens); 
    lstOut.Items.Add("Fives: " + fives); 
    lstOut.Items.Add("Ones: " + singles); 
} 
+0

如果我的帳戶不是太新,我會加註。也謝謝你! –