C# 如何检查数组中是否包含数字并获取其索引?
如果我输入数字5,它位于索引4,它将在前3次给我C# 如何检查数组中是否包含数字并获取其索引?,c#,C#,如果我输入数字5,它位于索引4,它将在前3次给我notfound,然后索引将与我输入的数字相同 int[] A = { 3, 6, 4, 9, 10, 1, 2, 8 }; int myNumber; int length = A.Length; Console.WriteLine("enter your number"); myNumber = Convert.ToInt32(Console.ReadLine()); for (int i = 0; i < length; i++) {
notfound
,然后索引将与我输入的数字相同
int[] A = { 3, 6, 4, 9, 10, 1, 2, 8 };
int myNumber;
int length = A.Length;
Console.WriteLine("enter your number");
myNumber = Convert.ToInt32(Console.ReadLine());
for (int i = 0; i < length; i++)
{
if (myNumber == A[i])
{
Console.WriteLine("the numer" + myNumber + "is present in the array at the index" +" "+ A[i]);
}
else
{
Console.WriteLine("the number you entered are not found");
}
Console.ReadKey();
}
int[]A={3,6,4,9,10,1,2,8};
int myNumber;
int长度=A.长度;
Console.WriteLine(“输入您的号码”);
myNumber=Convert.ToInt32(Console.ReadLine());
for(int i=0;i
正确的程序:
int[] A = { 3, 6, 4, 9, 10, 1, 2, 8 };
int myNumber;
int length = A.Length;
Console.WriteLine("enter your number");
myNumber = Convert.ToInt32(Console.ReadLine());
// ADDED
bool found = false;
for (int i = 0; i < length; i++)
{
if (myNumber == A[i])
{
found = true; // ADDED
// On the far right of next row: Fixed A[i] -> i
Console.WriteLine("the numer" + myNumber + "is present in the array at the index" + " " + i);
break;
}
}
// ADDED
if (!found)
{
Console.WriteLine("the number you entered are not found");
}
Console.ReadKey();
int[]A={3,6,4,9,10,1,2,8};
int myNumber;
int长度=A.长度;
Console.WriteLine(“输入您的号码”);
myNumber=Convert.ToInt32(Console.ReadLine());
//增加
bool-found=false;
for(int i=0;ii
WriteLine(“数组中索引“+”+i”处存在数字“+myNumber+”);
打破
}
}
//增加
如果(!找到)
{
Console.WriteLine(“未找到您输入的号码”);
}
Console.ReadKey();
我希望/认为你能在没有帮助的情况下看到/理解这些差异
我要补充一点,解决这个问题还有第二种方法:
int[] A = { 3, 6, 4, 9, 10, 1, 2, 8 };
int myNumber;
int length = A.Length;
Console.WriteLine("enter your number");
myNumber = Convert.ToInt32(Console.ReadLine());
// MOVED OUTSIDE FOR
int i = 0;
for (; i < length; i++)
{
if (myNumber == A[i])
{
// On the far right of next row: Fixed A[i] -> i
Console.WriteLine("the numer" + myNumber + "is present in the array at the index" + " " + i);
break;
}
}
// ADDED
if (i == length)
{
Console.WriteLine("the number you entered are not found");
}
Console.ReadKey();
int[]A={3,6,4,9,10,1,2,8};
int myNumber;
int长度=A.长度;
Console.WriteLine(“输入您的号码”);
myNumber=Convert.ToInt32(Console.ReadLine());
//搬出去
int i=0;
对于(;ii
WriteLine(“数组中索引“+”+i”处存在数字“+myNumber+”);
打破
}
}
//增加
如果(i==长度)
{
Console.WriteLine(“未找到您输入的号码”);
}
Console.ReadKey();
查看差异:found
不是必需的,我们只使用扩展了“范围”的i
变量。正确的程序:
int[] A = { 3, 6, 4, 9, 10, 1, 2, 8 };
int myNumber;
int length = A.Length;
Console.WriteLine("enter your number");
myNumber = Convert.ToInt32(Console.ReadLine());
// ADDED
bool found = false;
for (int i = 0; i < length; i++)
{
if (myNumber == A[i])
{
found = true; // ADDED
// On the far right of next row: Fixed A[i] -> i
Console.WriteLine("the numer" + myNumber + "is present in the array at the index" + " " + i);
break;
}
}
// ADDED
if (!found)
{
Console.WriteLine("the number you entered are not found");
}
Console.ReadKey();
int[]A={3,6,4,9,10,1,2,8};
int myNumber;
int长度=A.长度;
Console.WriteLine(“输入您的号码”);
myNumber=Convert.ToInt32(Console.ReadLine());
//增加
bool-found=false;
for(int i=0;ii
WriteLine(“数组中索引“+”+i”处存在数字“+myNumber+”);
打破
}
}
//增加
如果(!找到)
{
Console.WriteLine(“未找到您输入的号码”);
}
Console.ReadKey();
我希望/认为你能在没有帮助的情况下看到/理解这些差异
我要补充一点,解决这个问题还有第二种方法:
int[] A = { 3, 6, 4, 9, 10, 1, 2, 8 };
int myNumber;
int length = A.Length;
Console.WriteLine("enter your number");
myNumber = Convert.ToInt32(Console.ReadLine());
// MOVED OUTSIDE FOR
int i = 0;
for (; i < length; i++)
{
if (myNumber == A[i])
{
// On the far right of next row: Fixed A[i] -> i
Console.WriteLine("the numer" + myNumber + "is present in the array at the index" + " " + i);
break;
}
}
// ADDED
if (i == length)
{
Console.WriteLine("the number you entered are not found");
}
Console.ReadKey();
int[]A={3,6,4,9,10,1,2,8};
int myNumber;
int长度=A.长度;
Console.WriteLine(“输入您的号码”);
myNumber=Convert.ToInt32(Console.ReadLine());
//搬出去
int i=0;
对于(;ii
WriteLine(“数组中索引“+”+i”处存在数字“+myNumber+”);
打破
}
}
//增加
如果(i==长度)
{
Console.WriteLine(“未找到您输入的号码”);
}
Console.ReadKey();
请看区别:
found
不是必需的,我们只使用扩展了“作用域”的i
变量。您希望将代码更改为这样的内容。第一个错误是打印[i]而不是i,第二个错误是在循环中打印未找到的消息。如果将其移到循环之外,它将只打印一次,这同样适用于Console.ReadKey()
int[]A={3,6,4,9,10,1,2,8};
int myNumber;
int长度=A.长度;
Console.WriteLine(“输入您的号码”);
myNumber=Convert.ToInt32(Console.ReadLine());
bool isFound=false;
for(int i=0;i
}您希望将代码更改为类似这样的内容。第一个错误是打印[i]而不是i,第二个错误是在循环中打印未找到的消息。如果将其移到循环之外,它将只打印一次,这同样适用于Console.ReadKey()
int[]A={3,6,4,9,10,1,2,8};
int myNumber;
int长度=A.长度;
Console.WriteLine(“输入您的号码”);
myNumber=Convert.ToInt32(Console.ReadLine());
bool isFound=false;
for(int i=0;i
}Array类有一个很好的索引方法,您可以使用它。它将返回您正在查找的值的索引位置,或者如果找不到该值,它将返回-1
int[] A = { 3, 6, 4, 9, 10, 1, 2, 8 };
Console.WriteLine("enter your number");
int myNumber = Convert.ToInt32(Console.ReadLine());
int indexLocation = Array.IndexOf(A, myNumber);
if (indexLocation > -1)
{
Console.WriteLine("The number {0} was found at index location {1}", myNumber, indexLocation);
}
else
{
Console.WriteLine("The number {0} was not found", myNumber);
}
Array类有一个很好的IndexOf方法,您可以使用它。它将返回您正在查找的值的索引位置,或者如果找不到该值,它将返回-1
int[] A = { 3, 6, 4, 9, 10, 1, 2, 8 };
Console.WriteLine("enter your number");
int myNumber = Convert.ToInt32(Console.ReadLine());
int indexLocation = Array.IndexOf(A, myNumber);
if (indexLocation > -1)
{
Console.WriteLine("The number {0} was found at index location {1}", myNumber, indexLocation);
}
else
{
Console.WriteLine("The number {0} was not found", myNumber);
}
索引是
i
nota[i]
索引是i
nota[i]
它应该是Array.IndexOf(a,myNumber)代码>。问题是,虽然它在技术上是正确的(也是解决它的最佳方法),但它不是“形成性的”。这是一个“这里的t”