我有一个项目,我必须在其中输入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个值,我需要使用数组而不是列表。

最佳答案

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

PS-在第一个答案上,我试图将我的建议代码尽可能地靠近您-在不更改代码的情况下回答您的问题。另一位评论者建议不断更新文本框。文本将导致闪烁的问题。如果您遇到这种情况,建议您在编辑文本时使用一个临时字符串。

我知道这是家庭作业-因此,我不建议您进行任何大的优化来使您看起来像在SO上完成家庭作业一样。

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

 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;

 }

关于c# - 显示多个阵列,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39511323/

10-12 00:11
查看更多