-4

嘿伙计们,我是编码的初学者,我正在用 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 有问题,但我不知道该怎么办。提前致谢。

4

1 回答 1

0

我认为您正在寻找Array.IndexOf

Int index = Array.IndexOf(amountOfArrays,guess);
If (index == -1)
{
    // not found
}
else 
{
    // found
}

Count++ 将是最后一个。你怎么期望它是别的东西?

于 2015-11-22T17:49:45.357 回答