【发布时间】: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)返回您需要在数组中查找的值吗? -
是的,它返回数组值,但我没有在输入中输入任何内容,只是一直单击确定..上面的语句有效。