显示多个数组

Ann*_*wry 6 c# arrays winforms

我有一个项目,我必须采取的输入names,weights以及heights并付诸阵列,并显示它们放入一个TextBox像这样的

name = "..."
weight = "..."
height= "..."
Run Code Online (Sandbox Code Playgroud)

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

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++;
    }
}
Run Code Online (Sandbox Code Playgroud)

该阵列最多可以存储10个值,我需要使用数组而不是列表.

Sha*_*ger 5

你应该:::::很多方法来优化你想要做的事情,但这是家庭作业,你不希望看起来你是世界上最伟大的程序员 - 你想做的项目就像教授期待你.因此,创建类和加入列表不是特定解决方案集的一部分.尝试:

PS - 在第一个答案时,我尽量保持我的建议代码尽可能接近你 - 在不改变你的代码的情况下回答你的问题.另一位意见提供者建议,不断更新textbox.Text将导致闪烁的问题.如果发生这种情况,我建议使用临时字符串,因为我已经编辑了我的文本.

我知道这是作业 - 所以我不建议任何大的优化会让你看起来像是在做你的作业.

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

 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;

 }
Run Code Online (Sandbox Code Playgroud)

注意文本框具有验证方法,这些方法将在我修订的编辑中执行我的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;

 }
Run Code Online (Sandbox Code Playgroud)