【问题标题】:Displaying Multiple Arrays显示多个数组
【发布时间】:2016-09-15 12:41:15
【问题描述】:

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

【问题讨论】:

标签: c# arrays winforms


【解决方案1】:

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

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

我知道这是功课 - 所以我不建议进行任何大优化,让您看起来像是在 SO 完成了功课。

EDIT 您要求提供一种检测空的方法。根据我对您的代码的理解并使其保持简单,请尝试:

 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 语句的工作以查找空。如果您认为 prof 正在寻找表单(控件)验证而不是代码隐藏 IF/THEN,请告诉我,我会提供帮助。

好的 - 你提到了排序的必要性。为此,我们需要使用某种方式对输入数据进行分组。我们可以使用字典或类。我们一起上课吧:

把它们放在一起:看看这个潜在的解决方案 - 如果你认为它对于你的家庭作业来说太复杂了,我们可以尝试简化。让我知道:

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;

 }

【讨论】:

  • 谢谢!这使订单正确,但换行符不起作用:(。编辑:没关系 \r\n 工作!谢谢!
  • 很高兴为您提供帮助!干杯。
  • 碰巧你知道如何让它忽略空值或空值吗?我知道有一个方法 isEmptyOrNull() 但我不知道如何在这里合并它。
  • 我认为修改后的答案应该有所帮助(请参阅我的编辑)。您可能想在消息框中添加按钮,但您会明白大致的想法......
  • 我刚刚再次编辑 - 我想您的教授可能希望查看文本框的表单验证,而不是代码隐藏。如果您正在学习表单验证,请告诉我,我们将朝那个方向发展,而不是我之前发布的 IF/THEN 语句。
【解决方案2】:

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

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());

【讨论】:

  • 谢谢回复,这不是用数组列表吗?当我开始使用数组列表时,我会试试这个:)。
  • @AnnieLowry 我使用 List 而不是数组,因为它是更通用的方式。如果您仍然想使用数组,只需将列表定义更改为数组就不会太难。
  • @AnnieLowry 检查我更新的答案。它现在使用数组而不是列表。
【解决方案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更改意味着重绘,因此闪烁

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-09
    • 1970-01-01
    • 2014-07-02
    • 1970-01-01
    • 1970-01-01
    • 2018-11-12
    • 1970-01-01
    • 2017-06-19
    相关资源
    最近更新 更多