2015-03-19 43 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