2013-05-01 159 views
1

我在GUI中製作計算器,我需要一些幫助。 當我在文本框中輸入一些數據時,我需要將它存儲在一個數組中。這就是我想到的。如何顯示數組元素,如果元素的數量是一個變量

int numOfPackages;//used to get user input 

    private void button3_Click(object sender, EventArgs e) 
    { 
     int[] weight = new int[numOfPackages]; 

     for(int i = 0; i < numOfPackages; i++) 
     { 
      weight[i] = Convert.ToInt32(weightBox.Text); 
     } 

     foreach (int i in weight) 
      totalCostLabel.Text = "" + weight[i]; 

    } 

,當我嘗試顯示的元素,它給我的indexOutOfRange例外。 那麼,我該如何顯示該數組的元素?

在此先感謝。

+3

請至少閱讀[關於文檔的foreach(http://msdn.microsoft.com/en-us/library/ttw7t8t6%28v=vs.100%29.aspx)。 – 2013-05-01 01:48:56

回答

5

此行

foreach (int i in weight) 
    totalCostLabel.Text = "" + weight[i]; 

應該是

foreach (int w in weight) 
    totalCostLabel.Text = "" + w; 

您當前的代碼迭代權重的陣列,並嘗試使用重量作爲一個指數到權重的陣列,引起指數出範圍例外。

的另一個問題是與第一循環:你的weight所有的值設置爲同一個號碼:

weight[i] = Convert.ToInt32(weightBox.Text); // That's the same for all i-s 

如果權重是不同的,他們應該來自不同重量箱,或從字符串單個weightBox應該以產生多個數字的方式處理(例如,通過使用string.Split)。

+0

哦,我沒有解釋,numOfPackages是用戶必須輸入的數字。但我增加了我,爲什麼它試圖將輸入存儲在相同的索引? – 2013-05-01 01:47:53

+0

@RomanGusan你應該通過編輯問題來解釋它。我期望'numOfPackages'來自另一個文本框,或者可能是一個不同的UI元素,對嗎? – dasblinkenlight 2013-05-01 01:50:07

0

這裏有多個問題。首先是這樣的:

foreach (int i in weight) 
     totalCostLabel.Text = "" + weight[i]; 

這是迭代weight數組並使用該數組中的每個值。然後,您將該值用作索引。看看下面的例子:

weight[0] = 0 
weight[1] = 1 
weight[2] = 15 

在代碼中,前兩個項目將工作,因爲那裏是0的指數爲1的指數不過當它到達最後一個條目,它尋找的索引15.你可以修復這兩種方法,第一種是使用正則for循環:

for(int i=0; i < weight.Length; i++) 
{ 
    totalCostLabel.Text += weight[i]; 
} 

這帶來了第二個錯誤。你不在代碼中追加任何東西到你的totalCostLabel中,你只是替換了值。這將把所有的重量值作爲一個附加在一起。

另一種方式做,這是使用foreach循環:

foreach(int i in weight) 
{ 
    totalCostLabel.Text += i; 
} 

這是與上面相同,但你不必擔心索引。底線,即使你修好你的循環後,你可能需要修復標籤接受文本的方式,否則你不會得到你想要的結果。

0

也許你想要更類似的東西?

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
     btnAdd.Enabled = false; 
    } 

    int[] weight; 
    int entriesMade; 
    int numOfPackages; 

    private void btnReset_Click(object sender, EventArgs e) 
    { 
     if (int.TryParse(numEntriesBox.Text, out numOfPackages)) 
     { 
      weight = new int[numOfPackages]; 
      entriesMade = 0; 
      btnReset.Enabled = false; 
      btnAdd.Enabled = true; 
      totalCostLabel.Text = ""; 
     } 
     else 
     { 
      MessageBox.Show("Invalid Number of Entries"); 
     } 
    } 

    private void btnAdd_Click(object sender, EventArgs e) 
    { 
     int value; 
     if (int.TryParse(weightBox.Text, out value)) 
     { 
      weight[entriesMade] = value; 
      weightBox.Clear(); 

      totalCostLabel.Text = ""; 
      int total = 0; 
      for (int i = 0; i <= entriesMade; i++) 
      { 
       total = total + weight[i]; 
       if (i == 0) 
       { 
        totalCostLabel.Text = weight[i].ToString(); 
       } 
       else 
       { 
        totalCostLabel.Text += " + " + weight[i].ToString(); 
       } 
      } 
      totalCostLabel.Text += " = " + total.ToString(); 

      entriesMade++; 
      if (entriesMade == numOfPackages) 
      { 
       btnAdd.Enabled = false; 
       btnReset.Enabled = true; 
       MessageBox.Show("Done!"); 
      } 
     } 
     else 
     { 
      MessageBox.Show("Invalid Weight"); 
     } 
    } 

} 
相關問題