C# 如何通知阵列具有特定数据点
好的,我这里的问题是,我试图创建一个用户输入数组,如果用户输入数组中有特定的数据点,则通知控制台。我知道你可以在控制台上看到数据点,但我被要求这样做,我没有问为什么:'DC# 如何通知阵列具有特定数据点,c#,arrays,C#,Arrays,好的,我这里的问题是,我试图创建一个用户输入数组,如果用户输入数组中有特定的数据点,则通知控制台。我知道你可以在控制台上看到数据点,但我被要求这样做,我没有问为什么:'D public static void Main() { double [] array= new double [10] double temp; Console.WriteLine("Insert 10 numbers"); for (int i = 0; i < array.Le
public static void Main()
{
double [] array= new double [10]
double temp;
Console.WriteLine("Insert 10 numbers");
for (int i = 0; i < array.Length; i++)
{
array[i] = Convert.ToDouble(Console.ReadLine());
}
Console.WriteLine("What number do you wish to find?");
temp = Convert.ToDouble(Console.ReadLine());
foreach (int i in array)
{
if (temp == i)
{
Console.WriteLine("The number is in the array.")
break;
}
else if (temp != i)
{
Console.WriteLine("The number is not part of the array.");
break;
}
}
}
输出是:如果您将第一个插槽中的数字放入,它将显示为数组中的数字,作为数组一部分的任何其他数字都将显示该数字不属于数组的一部分。如果删除If和Else If代码中的换行符,列表将逐一告诉您该数字是否为数组的一部分
我如何使它读取整个数组,并告诉我无论什么数字都是数组的一部分
注意,我被要求不要使用LINQ或数组。对象不应检查温度!=i、 但是==仅在循环内:
// array is double[] - item is double
foreach (double item in array) {
// item found, we shall report and exit the method
if (item == temp) { // <- dubious: we should check doubles with tolerance
Console.WriteLine("The number is in the array.");
return; // <- not break
}
}
// entire array has been scanned, no item found
Console.WriteLine("The number is not part of the array.");
bool inTheArray = false;
foreach (double item in array)
if (item == test) {
inTheArray = true;
break;
}
Console.WriteLine(inTheArray
? "The number is in the array."
: "The number is not part of the array.");