我的程序没有编译错误,但输出不正确。示例性输入:阵列的线性搜索问题
大小:5张
输入数:5 4 3 2 1
//排序:1 2 3 4 5
搜索:1
输出:在索引中找到数1 4
由于已经对数字进行了排序,因此输出应该是索引0处的数字1。我将如何改变它。
int[] nums = new int[100];
int SizeNum;
bool isNum = false;
private void ExeButton_Click(object sender, EventArgs e)
{
int i, loc, key;
Boolean found = false;
string SizeString = SizeTextBox.Text;
isNum = Int32.TryParse(SizeString, out SizeNum);
string[] numsInString = EntNum.Text.Split(' '); //split values in textbox
for (int j = 0; j < numsInString.Length; j++)
{
nums[j] = int.Parse(numsInString[j]);
}
if (SizeNum == numsInString.Length)
{
Array.Sort(numsInString);
key = int.Parse(SearchTextBox.Text);
ResultText.AppendText("Sorted: ");
for (i = 0; i < SizeNum; i++)
ResultText.AppendText(" " + numsInString[i]);
ResultText.AppendText("\n\n");
{
for (loc = 0; loc < SizeNum; loc++)
{
if (nums[loc] == key)
{
found = true;
break;
}
}
if (found == true)
ResultText.AppendText("Number " + key + " Found At Index [" + loc + "]\n\n");
else
ResultText.AppendText("Number " + key + " Not Found!\n\n");
}
}
}
这看起来像什么,但线性搜索。 –
你的程序清楚*不会*有错误,因为它不会做你想要的... –
它看起来很像线性搜索。最后一部分是。 Sort()并不是必需的,如果你想要在输入数据中的位置,这是错误的。 –