嘿伙计们我是编码的初学者,我在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();
我的计数int有问题,但我不知道该怎么做。提前谢谢。
答案 0 :(得分:0)
我认为您正在寻找Array.IndexOf
Int index = Array.IndexOf(amountOfArrays,guess);
If (index == -1)
{
// not found
}
else
{
// found
}
Count ++将是最后一个。你怎么看待它是什么呢?