我有一个项目,我必须接受names
,weights
和heights
的输入并将它们放入数组并将其显示为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个值,我需要使用数组而不是列表。
答案 0 :(得分: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;
}
注意文本框具有验证方法,可以在我修改的编辑中执行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;
}
答案 1 :(得分: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());
答案 2 :(得分: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
更改意味着重绘,从而闪烁)