2015-03-19 155 views
-1

我在賦值時遇到問題,無法清除數組。同樣在我的MessageBox中,當它顯示分數時,無論我做什麼,我都會得到一個零作爲第一個數字。我不知道要改變什麼,所以零不是第一個元素。清除數組

public partial class Form1 : Form 
{ 
    int scoreTotal = 0; 
    int scoreCount = 0; 
    decimal average = 0; 
    int[] scoreTotalArray = new int[1]; 

    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void btnAdd_Click(object sender, EventArgs e) 
    { 
     try 
     { 
      if (IsValidData()) 
      { 
       Int32 score = Convert.ToInt32(txtScore.Text); 
       scoreTotalArray[scoreCount] = score; 
       Array.Resize(ref scoreTotalArray, scoreTotalArray.Length + 1); 
       scoreTotal += score; //accumulator 
       scoreCount++; //counter 
       average = scoreTotal/(decimal)scoreCount;//average 

       txtScoreCount.Text = scoreCount.ToString();//display in score count txtbox 
       txtScoreTotal.Text = scoreTotal.ToString(); //display in score total txtbox 
       txtAverage.Text = average.ToString("n2"); //display in average text box 

       txtScore.Clear(); 
       txtScore.Focus(); 
      } 
     } 
     catch (Exception ex) //catches all other exceptions 
     { 
      MessageBox.Show(ex.Message, ex.GetType().ToString()); 
     } 
    } 

    public bool IsValidData() 
    { 
     return 
      IsPresent(txtScore, "Score:") && 
      IsInt32(txtScore, "Score:") && 
      IsWithinRange(txtScore, "Score:", 0, 100); 
    } 

    public bool IsPresent(TextBox textBox, string name) 
    { 
     if (textBox.Text == "") 
     { 
      MessageBox.Show(name + " is a required field, please enter a number between 0 and 100.", "Entry Error"); 
      textBox.Focus(); 
      return false; 
     } 
     return true; 
    } 

    public bool IsInt32(TextBox textBox, string name) 
    { 
     try 
     { 
      Convert.ToInt32(textBox.Text); 
      return true; 
     } 
     catch (FormatException) 
     { 
      MessageBox.Show(name + "must be a number between 0 and 100.", "Entry Error"); 
      textBox.Focus(); 
      return false; 
     } 
    } 

    public bool IsWithinRange(TextBox textBox, string name, decimal min, decimal max) 
    { 
     decimal number = Convert.ToDecimal(textBox.Text); 
     if (number < min || number > max) 
     { 
      MessageBox.Show(name + " must be between " + min + " and " + max + ".", "Entry Error"); 
      textBox.Focus(); 
      return false; 
     } 
     return true; 
    } 

    private void btnDisplayScore_Click(object sender, EventArgs e) 
    { 
     Array.Sort(scoreTotalArray); 
     string scoreCountString = "\n"; 
     for(int i = 0; i < scoreCount; i++) 
     scoreCountString += scoreTotalArray [i] + "\n"; 

     MessageBox.Show(scoreCountString + "\n", "Sorted Scores"); 
    } 

    private void btnClearScores_Click(object sender, EventArgs e) 
    { 
     txtScore.Clear(); 
     txtScoreTotal.Clear(); 
     txtScoreCount.Clear(); 
     txtAverage.Clear(); 

     txtScore.Focus(); 
    } 
+1

清除數組?或者清除MessageBox?你能否澄清這個問題,並且縮小你認爲問題出在哪裏的所有代碼? – 2015-03-19 02:40:45

+0

對不起,我需要清除消息框,而且需要清除存儲的數據,所以如果用戶點擊「清除分數」按鈕,那麼他們可以重新開始,並且它不會繼續添加分數先前輸入。 – Ladybrinx 2015-03-19 02:42:30

回答

2

你的int數組scoreTotalArray總是一個大的元素。額外的元素包含0要擺脫;-)

清除分數可以做到通過調整你的陣列0

這就是說:你或許應該考慮使用List,而不是一個int陣列。

+0

我們不允許使用列表,我們必須使用一個數組,所以我一直在試圖弄清楚這一點。那麼我會在哪裏放置代碼來調整它的大小?它已經在那裏一次,不知道它應該在'btnClearScores_Click'中應該去 – Ladybrinx 2015-03-19 02:49:09

+0

,你應該添加:'Array.Resize(ref scoreTotalArray,0);' 設置'scoreTotal','scoreCount'和'average'爲' 0'。 替換爲:'int [] scoreTotalArray = new int [1];'帶此:'int [] scoreTotalArray = new int [0];' 在'btnAdd_Click'中,切換這兩行的順序: 'scoreTotalArray [scoreCount] = score;' 'Array.Resize(ref scoreTotalArray,scoreTotalArray.Length + 1);' – KompjoeFriek 2015-03-19 02:56:25

+1

您應該也許應該使用'scoreTotalArray.Length'而不是'scoreCount'。記住數組中的最後一項是'array [array.Length - 1]'。 – 2015-03-19 03:03:48