2016-09-15 104 views
6

我有一个项目,我不得不采取在namesweightsheights输入并付诸阵列,并将它们显示为TextBox这样显示多个阵列

name = "..." 
weight = "..." 
height= "..." 

我已经能够填充我的数组,但我不明白如何输出它像上面的例子。目前我的输出是所有名称,那么所有的权重,然后所有的高度。有人可以解释我将如何使它能够像例子一样显示吗?我到目前为止的代码是

private void ShowButton_Click(object sender, EventArgs e) 
{   
    txtShow.Text += string.Join(System.Environment.NewLine, nameArray); 
    txtShow.Text += string.Join(System.Environment.NewLine, weightArray); 
    txtShow.Text += string.Join(System.Environment.NewLine, heightArray);  
} 
private void AddButton_Click(object sender, EventArgs e) 
{ 
    if (this.index < 10) 
    { 
     nameArray[this.index] = nameBox.Text; 
     weightArray[this.index] = double.Parse(weightBox.Text); 
     heightArray[this.index] = double.Parse(heightBox.Text); 
     this.index++; 
    } 
} 

阵列最多可存储10个值,我需要使用数组,而不是名单。

+1

那么,你可以改变'nameArray'到'string.Concat(nameArray)'等。 – Fabjan

+0

谢谢您的回复! –

+1

您正在讨论多维数组,尽管下面的所有答案都是正确的,但这可能是您需要的最充分的答案。 https://www.dotnetperls.com/multidimensional-array或这里https://msdn.microsoft.com/en-us/library/2yd9wwz4.aspx –

回答

5

您应该::::::很多方法可以优化你想要做的事情,但这是作业,你不想看起来像你是世界上最伟大的程序员 - 你想要做像教授期待你那样的项目。因此创建类和连接列表不属于您的特定解决方案集。尝试:

PS - 在第一个答案上,我尽量保持我的建议代码尽可能接近您的意见 - 在不更改代码的情况下回答您的问题。另一位评论者建议,不断更新文本框。文本将导致闪烁的问题。如果发生这种情况,我会建议在编辑文本时使用临时字符串。

我知道这是家庭作业 - 所以我不建议任何大的优化,这会让你看起来像你一直在做你的功课完成。

编辑您已经要求检测空白的方法。根据我对你的代码的理解,并保持它的简单,尝试:

private void AddButton_Click(object sender, EventArgs e) 
{ 
    if (this.index < 10) 
    { 
     if(nameBox.Text.Length==0||weightBox.Text.Length==0||heightBox.Text.Length==0){ 
      MessageBox.Show("You must enter a name, weight, and height!"); 
     }else{ 
      nameArray[this.index] = nameBox.Text; 
      weightArray[this.index] = double.Parse(weightBox.Text); 
      heightArray[this.index] = double.Parse(heightBox.Text); 

      this.index++; 
      nameBox.Text = ""; 
      weightBox.Text = ""; 
      heightBox.Text = ""; 
     } 
    } 
} 
private void ShowButton_Click(object sender, EventArgs e) 
{  string myString = ""; 
     for(int i=0;i<nameArray.Length;i++) 
     { 
       myString+= "Name: "+nameArray[i]+", "; 
       myString += "Weight: "+weightArray[i]+", "; 
       myString += "Height: "+heightArray[i]+"\n"); 
     } 
     txtShow.Text = myString; 

} 

注意文本框必须验证方法将做我的修订编辑我的IF/THEN语句的工作找空瓶。如果您认为教授正在寻找形式(控制)验证而不是IF/THEN后面的代码隐藏,请告诉我,我会为您提供帮助。

好的 - 你提到需要排序。要做到这一点,我们需要使用某种方法来分组输入数据。我们可以使用Dictionary或class。让我们一起去上课:

把它放在一起:看看这个潜在的解决方案 - 如果你觉得它太复杂了,你的作业应该看起来像,我们可以尝试简化。告诉我:

public class Person{ 
    public string Name {get;set;} 
    public double Height {get;set;} 
    public double Weight {get; set;} 
    public string Print(){ 
     return "Name: "+Name+", Height: "+Height.ToString()+", Weight: "+Weight.ToString()+"\r\n"; 
    } 
} 
Person[] People = new Person[10]; 
int thisIndex = 0; 
private void AddButton_Click(object sender, EventArgs e) 
    { 
     if (this.index < 10) 
    { 
     if(nameBox.Text.Length==0||weightBox.Text.Length==0||heightBox.Text.Length==0) 
     { 
      MessageBox.Show("You must enter a name, weight, and height!"); 
     }else{ 
      Person p = new Person(); 
      p.Name = nameBox.Text; 
      p.Weight = double.Parse(weightBox.Text); 
      p.Height = double.Parse(heightBox.Text); 
      People[thisIndex] = p; 
      thisIndex++; 
      nameBox.Text = ""; 
      weightBox.Text = ""; 
      heightBox.Text = ""; 
     } 
    } 
} 
private void ShowButton_Click(object sender, EventArgs e) 
{  
     People = People.OrderBy(p=>p.Name).ToArray(); 
     string myString = ""; 
     for(int i=0;i<10;i++) 
     { 
      if(People[I]!=null){ 
       myString+= People[I].Print(); 
      } 
     } 
     txtShow.Text = myString; 

} 
+0

谢谢!这使命令正确,但换行符不工作:(编辑:无意中\ r \ n工作!谢谢! –

+1

很高兴帮助!干杯。 –

+0

偶然你知道如何让它忽略空值或空值?我知道有一种方法isEmptyOrNull(),但我不知道如何将它合并到这里。 –

3

你应该为此创建一个类。一个类,您可以通过三种类型(字符串,双和双)的变量组合在一起来创建自己的定制类型:

public class Person 
{ 
    public string Name { get; set; } 
    public double Weight { get; set; } 
    public double Height { get; set; } 

    public override string ToString() 
    { 
     return Name + " " + Weight + " " + Height; 
    } 
} 

然后:

Person[] persons = new Person[10]; 
private void AddButton_Click(object sender, EventArgs e) 
{ 
    persons[index] = new Person 
     { 
      Name = nameBox.Text, 
      Weight = double.Parse(weightBox.Text), 
      Height = double.Parse(heightBox.Text) 
     }; 
     index++; 
} 

最后(看ShowButton的代码现在是一行):

txtShow.Text += string.Join(System.Environment.NewLine, persons.AsEnumerable()); 
+0

谢谢你的回复,但不是使用数组列表?当我使用arraylist时,我会尝试这个:)。 –

+0

@AnnieLowry我用列表而不是数组,因为它是更通用的方式。如果你仍然想使用数组,不应该太难,只需将列表定义更改为数组。 –

+0

好的,谢谢!我也会尝试这种方式。 –

3

LINQ的溶液:

private void ShowButton_Click(object sender, EventArgs e) { 
    int n = Math.Min(Math.Min(nameArray.Length, weightArray.Length), heightArray.Length)); 

    txtShow.Text = String.Join(Environment.NewLine, Enumerable 
    .Range(0, n) 
    .Select(i => string.Format("Name: {0}, Weight: {1}, Height: {2}", 
     nameArray[i], weightArray[i], heightArray[i]))); 
} 

尝试避免txtShow.Text +=片段,特别是在环(每个Text改变装置重绘因此闪烁

+0

我试过了,它也有效。谢谢! –

+0

@安妮洛瑞:不客气! –