【问题标题】:Searching for values in arrays在数组中搜索值
【发布时间】:2019-02-25 15:47:43
【问题描述】:

我在 C# 中有一个数组,它生成长度为 5 的随机数组。我已经这样声明了

int[] array = new int[5]

我应该搜索一个数组,一个输入对话框打开,然后我输入任何值。如果找到数字,它应该给我一个输出,否则显示未找到并继续运行,直到我输入正确的数字。

我有这样的代码,它给了我一些不是我需要的值。我怎样才能实现它以满足我的条件?提前致谢。

    private void btnSearch_Click(object sender, EventArgs e)
    {
        //use InputBox dialog to input a value.
        //Search for the value in the array.
        //If found, display "Found!", otherwise
        //display "Not found!" when there is no match.

        for (int i = 0; i < array.Length; i++)
        {
            InputBox inputDlg = new InputBox("Search for array value " + (i + 1));
            if (inputDlg.ShowDialog() == DialogResult.OK)
            {
                if (array[i] == Array.IndexOf(array, 5))
                {
                    array[i] = Convert.ToInt32(inputDlg.Input);
                }
                tbxOutput.AppendText("Value found: " + array[i] + Environment.NewLine);
            }

            else
            {
                tbxOutput.AppendText("Value not found" + Environment.NewLine);
            }
        }

【问题讨论】:

  • Convert.ToInt32(inputDlg.Input) 返回您需要在数组中查找的值吗?
  • 是的,它返回数组值,但我没有在输入中输入任何内容,只是一直单击确定..上面的语句有效。

标签: c# arrays for-loop search


【解决方案1】:

如果我理解正确,您有 1 个数组,其中包含 5 个值,并且您想检查它是否包含给定值。那是对的吗? 如果是,则必须遍历数组并将布尔值标记为 true(如果找到):

private void btnSearch_Click(object sender, EventArgs e) {

    // loop until you call break
    while(true) {

        // ask for a value
        InputBox inputDlg = new InputBox("Search for array value " + (i + 1));

        try {
            int value = Convert.ToInt32(inputDlg.Input);

            // Check if value is in the array and display the appropriate message
            if(isInArray(array, value)) {
                tbxOutput.AppendText("Value found: " + value + Environment.NewLine);
                // break to exit from the while loop
                break;
            } else {
                tbxOutput.AppendText("Value not found" + Environment.NewLine);
            } 

        } catch (OverflowException) {
            tbxOutput.AppendText("OverflowException parsing input to int" + Environment.NewLine);
        } catch (FormatException) {
            tbxOutput.AppendText("FormatException parsing input to int" + Environment.NewLine);
        }   

    }
}

isInArray 方法:

// this method returns true if the given value is in the array
private static boolean isInArray(int[] array, int valueToFind) {
    boolean found = false;
    int currentValue;
    for (int i = 0; i < array.Length; i++) {
        currentValue = array[i];
        if(currentValue == valueToFind) {
            found = true;
            break;
        }
    }
    return found;
}

【讨论】:

  • 是的。但在此之前会打开一个对话框,要求用户输入一个数字。如果没问题,它将显示找到,否则继续循环直到找到正确的数字。我使用 Windows 窗体应用程序而不是 Windows 控制台。 (inputDlg.ShowDialog() == DialogResult.OK)
  • @Ralph 我编辑了答案以包含循环和对话框
  • isInArray 中有一个 for 循环。对于另一个循环,您不能使用for 循环,除非您希望尝试次数有限。 while 是在输入正确值之前不断询问的唯一方法。
  • @Ralph 我编辑了我的答案,你现在在 isInArray 方法中有那一行
  • @Ralph 你用的是什么语言?是 Java 还是 C# 还是其他?
猜你喜欢
  • 2022-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多