【发布时间】:2017-10-20 09:06:51
【问题描述】:
我正在尝试搜索一个数组以找到另一个较小的数组值。我使用了嵌套循环,找到的值完美显示。但是,我还想打印未找到的值,谁能帮我改进我的代码,以便在循环中打印未找到的值,而不像我在代码中那样使用文字(代码块//改进代码)
希望任何人都可以提供帮助。
( class Program
{
static void Main(string[] args)
{
int[] intArray = { 5, 12, 0, 67, 75, 3, 27, 1, 98};
int[] searchValues = { 0, 25, 99, 12, 3 };
for (int i = 0; i < searchValues.Length; ++i)
{
for (int j = 0; j< intArray.Length; ++j)
{
if (searchValues[i]== intArray[j])
{
Console.WriteLine("The Value {0} Has been found in index {1} of intarray ", searchValues[i], j);
}
}
// improve the code
if (i == 1 || i == 2)
{
Console.WriteLine("The Value {0} was Not found in intarray ", searchValues[i]);
}
}
Console.Read();
}
})
【问题讨论】: