c#中的线性搜索
Linear search in c#
大家好,我是编码初学者,我正在用 c# 进行线性搜索,但无法弄清楚如何让它在搜索时显示在哪个数组中找到的数字。它只是说它在最后一个数组上。这是我的代码:
Random Generator = new Random();
int[] array = new int[100];
int count = 0;
for (int i = 0; i < array.Length; i++)
{
array[i] = Generator.Next(1, 100);
count++;
}
Console.WriteLine("Write a number between 1-100 and see if it's in a array and in what array.");
int guess = Convert.ToInt32(Console.ReadLine());
bool exists = array.Contains(guess);
if (exists)
{
Console.WriteLine("Your number {0} is in the {1}th Array.", guess, count);
}
else
{
Console.WriteLine("Your number {0} does not match any number in any of the Arrays.", guess);
}
Console.ReadKey();
我的 count int 有问题,但我不知道该怎么办。提前致谢。
我想你在找 Array.IndexOf
Int index = Array.IndexOf(amountOfArrays,guess);
If (index == -1)
{
// not found
}
else
{
// found
}
Count++ 将是最后一个。你怎么会期望它是其他任何东西?
大家好,我是编码初学者,我正在用 c# 进行线性搜索,但无法弄清楚如何让它在搜索时显示在哪个数组中找到的数字。它只是说它在最后一个数组上。这是我的代码:
Random Generator = new Random();
int[] array = new int[100];
int count = 0;
for (int i = 0; i < array.Length; i++)
{
array[i] = Generator.Next(1, 100);
count++;
}
Console.WriteLine("Write a number between 1-100 and see if it's in a array and in what array.");
int guess = Convert.ToInt32(Console.ReadLine());
bool exists = array.Contains(guess);
if (exists)
{
Console.WriteLine("Your number {0} is in the {1}th Array.", guess, count);
}
else
{
Console.WriteLine("Your number {0} does not match any number in any of the Arrays.", guess);
}
Console.ReadKey();
我的 count int 有问题,但我不知道该怎么办。提前致谢。
我想你在找 Array.IndexOf
Int index = Array.IndexOf(amountOfArrays,guess);
If (index == -1)
{
// not found
}
else
{
// found
}
Count++ 将是最后一个。你怎么会期望它是其他任何东西?