【问题标题】:Sequential Search inside of array issue数组问题内的顺序搜索
【发布时间】:2015-10-22 14:36:29
【问题描述】:
    string[] names = { "Abby", "Bill", "Connie", "David", "Eddy" };

    private void clearButton_Click(object sender, EventArgs e)
    {
        //Clear the array labels

        array1Label.Text = "";
        array2Label.Text = "";
    }

    private void passArrayButton_Click(object sender, EventArgs e)
    {
        //Demo how to pass an array
        //Which really means passing the reference vairable to the array.

        //Create an array

        int[] array1 = {10, 20, 30, 40, 50};

        //Pass array1 to a method to double and dsiplay the array

        DoubleArray(array1);

    }

    private void DoubleArray(int[] theArray1)
    {
        string output = "";
        for (int x = 0; x < theArray1.Length; x++)
        {
            theArray1[x] = theArray1[x] * 2;
        }

        //display the array

        foreach(int element in theArray1)
        {
            output = output + element + "\n";
        }

        array1Label.Text = output;

    }

    private void showNameButton_Click(object sender, EventArgs e)
    {         
        string[] names = { "Abby", "Bill", "Connie", "David", "Eddy" };           
    }     
}

对如何使用顺序搜索能够将“David”输出到标签上深感困惑,请帮忙。这是我的整个程序,第一部分只是将所有数字相乘。请帮忙!没有外部输入,只需要通过“names”数组,挑出“David”,将文本输出到标签中即可。

【问题讨论】:

  • 您在寻找MyLabel.Text = names.Contains("David") ? "David" : "";吗?
  • 旁注:array1Label.Text = String.Join(Environment.NewLine, theArray1); 是您的 foreach 循环 的更好选择

标签: c# arrays sequential


【解决方案1】:

:)

private void showNameButton_Click(object sender, EventArgs e)
{
    string searchKey ="David";
    string output = "";
    string[] names = { "Abby", "Bill", "Connie", "David", "Eddy"};
    foreach(var name in names)
    {
       if(name == searchKey){
           output = name;
           break;
        }
    }
    array1Label.Text = output;
}         

将任何内容放入 searchkey 以查找除 David 之外的任何内容。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-11
    相关资源
    最近更新 更多